Sinch空指针问题

时间:2015-08-22 08:37:26

标签: java android sinch

在logcat中获取此错误:

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法'布尔值   com.demo.SinchService $ SinchServiceInterface.isStarted()'在null上   对象参考               在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() {

    }

}

2 个答案:

答案 0 :(得分:2)

这是有问题的一行:

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

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

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

答案 1 :(得分:1)

这将返回一个空引用:

 getSinchServiceInterface()

如果您使用的是方法,可以发布上述方法的代码吗?

如果它是一个Sinch Library方法,那么看看为什么它返回一个null。