13

我正在使用 android 支持库 v4 修订版 11,并且不时地看到下面的堆栈跟踪;我怀疑这是由于最近的发布而发生的,因此我尝试访问的模型为空。无论如何,这个跟踪让我想知道为什么在销毁活动时调用 onCreateView ,以及处理这种情况的最佳方法是什么?

java.lang.NullPointerException
    at com.example.dialogs.ExampleDialogFragment.onCreateView(ExampleDialogFragment.java:53)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
    at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(FragmentManager.java:1888)
    at android.support.v4.app.FragmentActivity.onReallyStop(FragmentActivity.java:787)
    at android.support.v4.app.FragmentActivity.doReallyStop(FragmentActivity.java:764)
    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:322)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
    at android.app.ActivityThread.access$2900(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    at dalvik.system.NativeStart.main(Native Method)

崩溃后,来自后台堆栈的活动被恢复,我会检查模型在活动中是否有效onResume()onStart()并且onCreate(Bundle savedInstanceState)。如果模型无效,我会从那里开始另一个活动FLAG_ACTIVITY_NO_HISTORY,调用finish()return;例如

@Override
public void onResume() {        
    Model cm = Application.getModel();
    final boolean isModelAvailable = cm != null;
    if (!isModelAvailable) {
        Intent restartIntent = IntentUtil.intentForAction(Constants.INTENT_RESTART);
        restartIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(restartIntent);
        finish();
        super.onResume();
        return;
    } else {

    }

INTENT_RESTART活动完成重新初始化模型后,它开始使用标志活动FLAG_ACTIVITY_CLEAR_TOP,此活动将事务提交到片段FirstFragment

使用 . 查看日志FragmentManager.enableDebugLogging(true);

05-13 13:24:53.051: V/FragmentManager(7468): Commit: BackStackEntry{40710fa0}
05-13 13:24:53.111: V/FragmentManager(7468): Commit: BackStackEntry{40771e78}
05-13 13:24:53.151: V/FragmentManager(7468): Run: BackStackEntry{40710fa0}
05-13 13:24:53.151: V/FragmentManager(7468): add: RetainFragment{4077d048 app_ImageCache}
05-13 13:24:53.151: V/FragmentManager(7468): Allocated fragment index RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:24:53.151: V/FragmentManager(7468): moveto CREATED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:24:53.151: V/FragmentManager(7468): moveto ACTIVITY_CREATED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:24:53.151: V/FragmentManager(7468): Run: BackStackEntry{40771e78}
05-13 13:24:53.151: V/FragmentManager(7468): add: FirstFragment{4077dfa8 id=0x7f04000d}
05-13 13:24:53.151: V/FragmentManager(7468): Allocated fragment index FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:24:53.151: V/FragmentManager(7468): moveto CREATED: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:24:53.151: V/FragmentManager(7468): moveto ACTIVITY_CREATED: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:24:53.791: V/FragmentManager(7468): moveto STARTED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:24:53.791: V/FragmentManager(7468): moveto STARTED: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:24:53.811: V/FragmentManager(7468): moveto RESUMED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:24:53.811: V/FragmentManager(7468): moveto RESUMED: FirstFragment{4077dfa8 #1 id=0x7f04000d}

05-13 13:25:58.414: V/FragmentManager(7468): Commit: BackStackEntry{40727530}
05-13 13:25:58.434: V/FragmentManager(7468): Run: BackStackEntry{4078fe00}
05-13 13:25:58.434: V/FragmentManager(7468): add: RetainFragment{4076f1b0 app_ImageCache}
05-13 13:25:58.434: V/FragmentManager(7468): Allocated fragment index RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:25:58.434: V/FragmentManager(7468): moveto CREATED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:25:58.434: V/FragmentManager(7468): moveto ACTIVITY_CREATED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:25:58.434: V/FragmentManager(7468): Run: BackStackEntry{40727530}
05-13 13:25:58.434: V/FragmentManager(7468): add: SecondFragment{40713c28 id=0x7f04000d}
05-13 13:25:58.434: V/FragmentManager(7468): Allocated fragment index SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:25:58.434: V/FragmentManager(7468): moveto CREATED: SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:25:58.434: V/FragmentManager(7468): moveto ACTIVITY_CREATED: SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:25:58.545: V/FragmentManager(7468): moveto STARTED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:25:58.545: V/FragmentManager(7468): moveto STARTED: SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:25:58.565: V/FragmentManager(7468): moveto RESUMED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:25:58.565: V/FragmentManager(7468): moveto RESUMED: SecondFragment{40713c28 #1 id=0x7f04000d}

05-13 13:26:02.338: V/FragmentManager(7468): Saved state of RetainFragment{4077d048 #0 app_ImageCache}: null
05-13 13:26:02.338: V/FragmentManager(7468): Saved state of FirstFragment{4077dfa8 #1 id=0x7f04000d}: Bundle[{android:view_state=android.util.SparseArray@40b15b60}]
05-13 13:26:02.338: V/FragmentManager(7468): saveAllState: adding fragment #0: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:26:02.338: V/FragmentManager(7468): saveAllState: adding fragment #1: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:26:02.348: V/FragmentManager(7468): movefrom STARTED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:26:02.348: V/FragmentManager(7468): movefrom STARTED: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:26:02.358: V/FragmentManager(7468): movefrom STOPPED: RetainFragment{4077d048 #0 app_ImageCache}
05-13 13:26:02.358: V/FragmentManager(7468): movefrom STOPPED: FirstFragment{4077dfa8 #1 id=0x7f04000d}
05-13 13:27:15.219: V/FragmentManager(7468): movefrom RESUMED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:27:15.219: V/FragmentManager(7468): movefrom RESUMED: SecondFragment{40713c28 #1 id=0x7f04000d}

05-13 13:27:15.239: V/FragmentManager(7468): Commit: BackStackEntry{40a8a6d0}
05-13 13:27:15.259: V/FragmentManager(7468): Commit: BackStackEntry{40a90190}

05-13 13:27:15.279: V/FragmentManager(7468): Run: BackStackEntry{40a8a6d0}
05-13 13:27:15.279: V/FragmentManager(7468): add: RetainFragment{40a7fdf0 app_ImageCache}
05-13 13:27:15.279: V/FragmentManager(7468): Allocated fragment index RetainFragment{40a7fdf0 #0 app_ImageCache}
05-13 13:27:15.279: V/FragmentManager(7468): moveto CREATED: RetainFragment{40a7fdf0 #0 app_ImageCache}
05-13 13:27:15.279: V/FragmentManager(7468): moveto ACTIVITY_CREATED: RetainFragment{40a7fdf0 #0 app_ImageCache}
05-13 13:27:15.279: V/FragmentManager(7468): Run: BackStackEntry{40a90190}
05-13 13:27:15.279: V/FragmentManager(7468): add: ThirdFragment{406c2308 id=0x7f04000d}
05-13 13:27:15.279: V/FragmentManager(7468): Allocated fragment index ThirdFragment{406c2308 #1 id=0x7f04000d}
05-13 13:27:15.289: V/FragmentManager(7468): moveto CREATED: ThirdFragment{406c2308 #1 id=0x7f04000d}
05-13 13:27:15.289: V/FragmentManager(7468): moveto ACTIVITY_CREATED: ThirdFragment{406c2308 #1 id=0x7f04000d}
05-13 13:27:15.410: V/FragmentManager(7468): moveto STARTED: RetainFragment{40a7fdf0 #0 app_ImageCache}
05-13 13:27:15.410: V/FragmentManager(7468): moveto STARTED: ThirdFragment{406c2308 #1 id=0x7f04000d}
05-13 13:27:15.430: V/FragmentManager(7468): moveto RESUMED: RetainFragment{40a7fdf0 #0 app_ImageCache}
05-13 13:27:15.430: V/FragmentManager(7468): moveto RESUMED: ThirdFragment{406c2308 #1 id=0x7f04000d}
05-13 13:27:15.810: V/FragmentManager(7468): Saved state of RetainFragment{4076f1b0 #0 app_ImageCache}: null
05-13 13:27:15.810: V/FragmentManager(7468): Saved state of SecondFragment{40713c28 #1 id=0x7f04000d}: Bundle[{android:view_state=android.util.SparseArray@40aa62c0}]
05-13 13:27:15.810: V/FragmentManager(7468): saveAllState: adding fragment #0: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:27:15.810: V/FragmentManager(7468): saveAllState: adding fragment #1: SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:27:15.810: V/FragmentManager(7468): movefrom STARTED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:27:15.810: V/FragmentManager(7468): movefrom STARTED: SecondFragment{40713c28 #1 id=0x7f04000d}
05-13 13:27:15.820: V/FragmentManager(7468): movefrom STOPPED: RetainFragment{4076f1b0 #0 app_ImageCache}
05-13 13:27:15.820: V/FragmentManager(7468): movefrom STOPPED: SecondFragment{40713c28 #1 id=0x7f04000d}


// Here I'm waiting for a while to make sure Android has enough time to do anything it wants, then I press a button gorse-closing the app  
05-13 13:27:59.783: D/AndroidRuntime(7468): Shutting down VM


05-13 13:28:14.928: V/FragmentManager(7523): Instantiated fragment RetainFragment{406cde80 #0 app_ImageCache}
05-13 13:28:14.938: V/FragmentManager(7523): restoreAllState: active #0: RetainFragment{406cde80 #0 app_ImageCache}
05-13 13:28:14.938: V/FragmentManager(7523): Instantiated fragment FirstFragment{406d8410 #1 id=0x7f04000d}
05-13 13:28:14.938: V/FragmentManager(7523): restoreAllState: active #1: FirstFragment{406d8410 #1 id=0x7f04000d}
05-13 13:28:14.938: V/FragmentManager(7523): restoreAllState: added #0: RetainFragment{406cde80 #0 app_ImageCache}
05-13 13:28:14.938: V/FragmentManager(7523): restoreAllState: added #1: FirstFragment{406d8410 #1 id=0x7f04000d}
05-13 13:28:14.938: V/FragmentManager(7523): moveto CREATED: RetainFragment{406cde80 #0 app_ImageCache}
05-13 13:28:14.938: V/FragmentManager(7523): moveto CREATED: FirstFragment{406d8410 #1 id=0x7f04000d}

05-13 13:28:15.128: V/msm8660.gralloc(7523): PmemUserspaceAllocator::PmemUserspaceAllocator(PmemUserspaceAllocator::Deps&, PmemUserspaceAllocator::Deps::Allocator&, const char*) begin
05-13 13:28:15.138: V/msm8660.gralloc(7523): PmemUserspaceAllocator::PmemUserspaceAllocator(PmemUserspaceAllocator::Deps&, PmemUserspaceAllocator::Deps::Allocator&, const char*) end
05-13 13:28:15.138: V/msm8660.gralloc(7523): PmemKernelAllocator::PmemKernelAllocator(PmemKernelAllocator::Deps&) begin
05-13 13:28:15.138: V/msm8660.gralloc(7523): PmemKernelAllocator::PmemKernelAllocator(PmemKernelAllocator::Deps&) end

05-13 13:28:15.408: V/FragmentManager(7523): moveto ACTIVITY_CREATED: RetainFragment{406cde80 #0 app_ImageCache}
05-13 13:28:15.408: V/FragmentManager(7523): moveto ACTIVITY_CREATED: FirstFragment{406d8410 #1 id=0x7f04000d}

java.lang.RuntimeException: Unable to destroy activity {com.myapp/com.myapp.activity.FirstFragment}: java.lang.NullPointerException

05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3272)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3299)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread.access$2100(ActivityThread.java:134)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1143)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.os.Looper.loop(Looper.java:152)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread.main(ActivityThread.java:4606)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at java.lang.reflect.Method.invoke(Method.java:491)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at dalvik.system.NativeStart.main(Native Method)

// see the original question log, it is quite the same
05-13 13:35:41.824: E/AndroidRuntime(7691): Caused by: java.lang.NullPointerException
05-13 13:35:41.824: E/AndroidRuntime(7691):     at com.myapp/com.myapp.activity.FirstFragment.onCreateView(FirstFragment.java:41)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(FragmentManager.java:1888)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentActivity.onReallyStop(FragmentActivity.java:787)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentActivity.doReallyStop(FragmentActivity.java:764)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:322)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at com.myapp.activity.FragmentsContainerActivity.onDestroy(FragmentsContainerActivity.java:806)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.Activity.performDestroy(Activity.java:4662)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1113)
05-13 13:35:41.824: E/AndroidRuntime(7691):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3255)
05-13 13:35:41.824: E/AndroidRuntime(7691):     ... 11 more

第 806 行FragmentsContainerActivitysuper.onDestroy();

@Override
public void onDestroy() {
    try {
        unregister(receiver);
    } catch (Exception e) {
    }
    super.onDestroy();
4

4 回答 4

7

按照此处的代码示例中的指导,onResume应该

总是先调用超类方法

这意味着你应该把你的super.onResume();权利放在开始。那一定是问题所在。

在您正在运行的当前代码中,您混淆了被告知停止(即finish)然后被告知停止的 FragmentActivity resume。在活动生命周期中不“合法”的生命周期。片段直接受此影响,因为它们的生命周期是从活动生命周期中调用的,如果活动从完成 -> 恢复它肯定意味着片段从完成/销毁 -> 恢复(通过 onCreateView)

于 2013-05-13T21:31:26.520 回答
1

如果没有看到代码,我们无法帮助您解决手头的具体问题。但是,其中一个事实是,它是一个 DialogFragment 是无关紧要的。

这就是说,根据我的经验,FragmentManager.enableDebugLogging()当您遇到生命周期问题时,这是一个救命稻草。例如,它会告诉你最后一个 moveToState 做了什么(即,它在什么状态之间移动),这可能会揭示一些错误。

于 2013-05-13T09:02:15.527 回答
1

对于您的具体问题,您是否尝试在调用 finish() 之前调用 super.resume() ?

否则你应该看看源代码: https ://github.com/mastro/android-support-library-archive/blob/23ad71b245e7b7bbde6a501671ab411725ce16f5/v4/src/java/android/support/v4/app/FragmentManager.java

从这个堆栈跟踪行 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)

于 2013-05-13T11:30:38.163 回答
0

请参阅这个类似的问题,它描述了您所看到的相同行为,但该解决方案可能不适用于您的情况。要解决您的问题,您可以简单地检查您的代码所依赖的任何内容是否onCreateViewnull.

于 2013-05-07T17:57:08.860 回答