来自通知会导致崩溃

时间:2015-01-14 00:38:27

标签: android

我的searchview有问题。如果用户来自通知,我的应用就会崩溃。

这是我的onResume

@Override
protected void onResume() {
    super.onResume();
    String userId = getIntent().getStringExtra("username");
    if(userId != null) 
    { 
        userId=userId+jidDomain;
        startChat(conversationsMap.get(userId));
    }
}

在第1015行之后的startChat方法中导致 nullPointerException

searchView.setIconified(true);

我在此方法中设置了searchView

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            searchItem = menu.findItem(R.id.action_search);
            SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
            searchView = (SearchView)searchItem.getActionView();
...

仅当用户来自通知时才会发生这种情况。如果用户正常启动应用程序,则不会发生这种情况。

崩溃报告:

01-14 00:31:49.211: E/AndroidRuntime(19507): java.lang.RuntimeException: Unable to resume activity {com.impact.app/com.impact.app.MainActivity}: java.lang.NullPointerException
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.os.Looper.loop(Looper.java:137)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at java.lang.reflect.Method.invoke(Method.java:525)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at dalvik.system.NativeStart.main(Native Method)
01-14 00:31:49.211: E/AndroidRuntime(19507): Caused by: java.lang.NullPointerException
01-14 00:31:49.211: E/AndroidRuntime(19507):    at com.impact.app.MainActivity.startChat(MainActivity.java:1015)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at com.impact.app.MainActivity.onResume(MainActivity.java:380)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.Activity.performResume(Activity.java:5211)
01-14 00:31:49.211: E/AndroidRuntime(19507):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
01-14 00:31:49.211: E/AndroidRuntime(19507):    ... 12 more

1 个答案:

答案 0 :(得分:0)

@Override
protected void onResume() {
    super.onResume();
    if (searchView != null) {
      chatStarted = true;
      startChat(...);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  ...
  searchView = (SearchView)searchItem.getActionView();
  if (!chatStarted) {
    chatStarted = true;
    startChat(...);
}