按下操作栏上的后退按钮时调用的onDestroy()

时间:2017-11-21 07:19:11

标签: android android-actionbar onbackpressed ondestroy

我有两个活动,第一个包含酒店列表的ListView enter image description here,第二个活动用于显示详细信息的RecyclerView RecyclerView

当我使用我的硬件返回按钮按下它时,我没有遇到问题,活动正常恢复,ListView仍然存在,但当我按下ActionBar上的后退按钮并且onDestroy()被调用时,我得到了NullPointerException

当我点击操作栏上的后退按钮时,如何防止调用onDestroy()?

编辑:
这是生命周期的logcat:
1. SearchResult init()


11-21 14:36:45.872 3037-3037/com.agranirwa.travel E/ActivityState: In the onStart() event
11-21 14:36:45.882 3037-3037/com.agranirwa.travel E/ActivityState: In the onResume() event

  1. 选择ListView项目时
  2. 
    11-21 14:37:55.922 3037-3037/com.agranirwa.travel E/ActivityState: In the onPause() event
    11-21 14:37:56.602 3037-3037/com.agranirwa.travel E/RecyclerView: No adapter attached; skipping layout
    11-21 14:37:56.732 3037-3037/com.agranirwa.travel E/RecyclerView: No adapter attached; skipping layout
    11-21 14:37:56.982 3037-3037/com.agranirwa.travel E/ActivityState: In the onStop() event
    

    3.硬件反压时

    
    11-21 14:38:14.562 3037-3037/com.agranirwa.travel E/ActivityState: In the onRestart() event
    11-21 14:38:14.562 3037-3037/com.agranirwa.travel E/ActivityState: In the onStart() event
    11-21 14:38:14.562 3037-3037/com.agranirwa.travel E/ActivityState: In the onResume() event
    

    4.当操作栏反向压缩时

    
    11-21 14:40:15.052 3037-3037/com.agranirwa.travel E/ActivityState: Activity is Finished
    11-21 14:40:15.052 3037-3037/com.agranirwa.travel E/ActivityState: In the onDestroy() event
    11-21 14:40:15.112 3037-3037/com.agranirwa.travel E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.agranirwa.travel, PID: 3037
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.agranirwa.travel/com.agranirwa.travel.ui.SearchResultActivity}: java.lang.NullPointerException
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                         Caused by: java.lang.NullPointerException
                                                                            at com.agranirwa.travel.ui.SearchResultActivity.onCreate(SearchResultActivity.java:100)
                                                                            at android.app.Activity.performCreate(Activity.java:5242)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:136) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5113) 
                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
                                                                            at dalvik.system.NativeStart.main(Native Method) 
    
    

1 个答案:

答案 0 :(得分:0)

也许你在ActionBar中的后退按钮有一些问题,你应该真正添加你的实际代码或实现这个解决方案

初始化ActionBar后添加

actionBar.setHomeButtonEnabled(true);

然后添加以下

@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{       
    switch (menuItem.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(menuItem);
    }
}