Sinch Service not starting in Android

1.9k views Asked by At

I was trying out the sample programs provided by Sinch and was getting a NullPointerException. I traced the code and found that it was caused by my device not connecting to the SinchService.

The code is as follows

public class LoginActivity extends BaseActivity implements SinchService.StartFailedListener {

    private Button mLoginButton;
    private EditText mLoginName;
    private ProgressDialog mSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        mLoginName = (EditText) findViewById(R.id.loginName);

        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setEnabled(false);
        mLoginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loginClicked();
            }
        });
    }

    @Override
    protected void onServiceConnected() {
        mLoginButton.setEnabled(true);
        getSinchServiceInterface().setStartListener(this);
    }

    @Override
    protected void onPause() {
        if (mSpinner != null) {
            mSpinner.dismiss();
        }
        super.onPause();
    }

    @Override
    public void onStartFailed(SinchError error) {
        Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show();
        if (mSpinner != null) {
            mSpinner.dismiss();
        }
    }

    @Override
    public void onStarted() {
        openPlaceCallActivity();
    }

    private void loginClicked() {
        String userName = mLoginName.getText().toString();

        if (userName.isEmpty()) {
            Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
            return;
        }

        if (!getSinchServiceInterface().isStarted()) {
            getSinchServiceInterface().startClient(userName);
            showSpinner();
        } else {
            openPlaceCallActivity();
        }
    }

    private void openPlaceCallActivity() {
        Intent mainActivity = new Intent(this, PlaceCallActivity.class);
        startActivity(mainActivity);
    }

    private void showSpinner() {
        mSpinner = new ProgressDialog(this);
        mSpinner.setTitle("Logging in");
        mSpinner.setMessage("Please wait...");
        mSpinner.show();
    }
}

I could see that since the service is not connecting I am not getting a correct output when I call getSinchServiceInterface(). It always returns null. My device is connected to the internet and the backend app in the Sinch dashboard seems to be working fine too.

LogCat:

 12-22 17:43:38.432 2301-2301/com.login_signup_screendesign_demo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.login_signup_screendesign_demo, PID: 2301
                                                                              java.lang.NullPointerException
                                                                                  at calling.LoginActivity.loginClicked(LoginActivity.java:78)
                                                                                  at calling.LoginActivity.access$000(LoginActivity.java:15)
                                                                                  at calling.LoginActivity$1.onClick(LoginActivity.java:32)
                                                                                  at android.view.View.performClick(View.java:4463)
                                                                                  at android.view.View$PerformClick.run(View.java:18770)
                                                                                  at android.os.Handler.handleCallback(Handler.java:808)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                  at android.os.Looper.loop(Looper.java:193)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

What could be the cause of this error and how to solve it?

UPDATE:

I exactly copy pasted the android code downloaded from this page. I am using sinch-rtc-sample-calling app from that file. I compiled and ran the app separately and it was working fine. Then I copy pasted all the java and xml from that app into the one which I am working and it is not working. Why is this happening. This might be the reason for the NullPointerException. After copy pasting I found that the login button is disabled. That means the onServiceConnected() is not executing. I tried calling it manually and that is resulting in the NPE, but that is happening correctly in the standalone app. I am getting these kind of problems only upon integration.

PS : I updated the manifest and gradle as per the new files.

2

There are 2 answers

7
knownUnknown On

everything looks fine, uncomment the commented part because there is one condition which is being checked and it returns

private boolean isStarted() { return (mSinchClient != null && mSinchClient.isStarted()); }

before getting SinchService and Also check your

private static final String APP_KEY = "app_key";
private static final String APP_SECRET = "app_secret";
private static final String ENVIRONMENT = "sandbox.sinch.com";

in SinchService.java, are the correct or not.

0
Mr.GT On

Have had the same issue over the past few days.

In my case it is because I hadn`t added the following in the manifest under application node

<service android:name=".SinchService"/>

Make sure that the setting is in your application manifest. That solved it for me.