在 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/