java - Sinch空指针问题

标签 java android sinch

在 logcat 中收到此错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.demo.SinchService$SinchServiceInterface.isStarted()' on a null object reference at com.demo.MainActivity.onCreate(MainActivity.java:54)

我的代码是:

public class MainActivity extends BaseActivity implements SinchService.StartFailedListener, View.OnClickListener{

SharedPreferences pref;
String ses_username,ses_email,ses_mobile;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pref= getApplicationContext().getSharedPreferences("com.demo", Context.MODE_PRIVATE); // 0 - for private mode
        ses_username=pref.getString("userName", null);
        ses_email=pref.getString("userEmail", null);
        ses_mobile=pref.getString("userMobile", null);


        if(ses_username !=null && ses_email !=null && ses_mobile !=null)
        {
            if (!getSinchServiceInterface().isStarted()) {
                String regId = pref.getString("regId", null);
                getSinchServiceInterface().registerPushNotification(regId.getBytes());
                getSinchServiceInterface().startClient(ses_mobile + "-" + ses_username.toLowerCase());
            }
            Intent in = new Intent(MainActivity.this,Workshop_Search.class);
            startActivity(in);
        }
}

    @Override
    public void onStartFailed(SinchError error) {

    }

    @Override
    public void onStarted() {

    }

}

最佳答案

这是有问题的行:

if (!getSinchServiceInterface().isStarted()) {

getSinchServiceInterface 由于某种原因返回 null,因此它没有 isStarted 方法,从而导致异常。您应该修复 getSinhServiceInterface,或者,如果它可能返回 null 是正确的,则修改 if:

if ((getSinchServiceInterface() == null) || (!getSinchServiceInterface().isStarted())) {

关于java - Sinch空指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154108/

相关文章:

java - ServletServerCache 和 ClassNotFoundException 之谜

android - 在 Android 中从 Firebase URL 在 VideoView 中显示视频

android - XmlPullParser 异常

ios - 如何消除 Sinch 中的依赖分析错误?

ios - 未通过网络应用程序从 Sinch 收到推送通知

java - Spring Jdbc模板+ MySQL = TransientDataAccessResourceException : Invalid Argument Value : Java. io.notSerializationException

Java,比较3个整数,排列最大,中值和最小

java - 当文本部分为空白并且按下按钮时(在 android studio 中),如何显示 toast 消息?

java - 从 OnClickListener 接口(interface)创建对象

android - Sinch 对应用程序到应用程序的调用收取费用