在logcat中获取此错误:
由: com.demo.SinchService$SinchServiceInterface.isStarted()‘引发:尝试在com.demo.MainActivity.onCreate(MainActivity.java:54)上对空对象引用调用虚拟方法的布尔java.lang.NullPointerException
我的守则是:
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() {
}
}发布于 2015-08-22 09:24:57
这是有问题的一行:
if (!getSinchServiceInterface().isStarted()) {getSinchServiceInterface由于某种原因返回null,因此它没有isStarted方法,从而导致异常。您应该修复getSinhServiceInterface,或者如果它可能返回null是正确的,则修改if
if ((getSinchServiceInterface() == null) || (!getSinchServiceInterface().isStarted())) {发布于 2015-08-22 08:42:32
这将返回一个空引用:
getSinchServiceInterface()如果这是你做的方法,你能把上面的方法的代码贴出来吗?
如果它是一个Sinch方法,那么看看它为什么返回null。
https://stackoverflow.com/questions/32154108
复制相似问题