我正在使用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
FragmentsContainerActivity
的第806行是super.onDestroy();
:
@Override
public void onDestroy() {
try {
unregister(receiver);
} catch (Exception e) {
}
super.onDestroy();
答案 0 :(得分:7)
按照code sample here中的指导,参考onResume
方法你应该:
始终先调用超类方法
意味着您应该在开始时将super.onResume();
置于正确的位置。
那一定是问题所在。
在您正在运行的当前代码中,您正在混淆被告知要停止的FragmentActivity(即finish
)然后被告知resume
。生命周期在活动生命周期中不是“合法的”。
片段直接受到影响,因为它们的生命周期是从活动生命周期中调用的,如果活动来自整理 - >恢复它肯定意味着碎片来自整理/破坏 - >恢复(通过onCreateView)。
答案 1 :(得分:1)
如果没有看到代码,我们无法帮助您解决手头的具体问题。但是,其中一个事实是,它是一个DialogFragment无关紧要。
那就是说,根据我的经验,当您遇到生命周期问题时,FragmentManager.enableDebugLogging()
可以挽救生命。例如,它会告诉你最后一次moveToState做了什么(即说它在它之间移动的是什么),这可能会对这个bug有所了解。
答案 2 :(得分:1)
对于您的具体问题,您是否在调用finish()之前尝试调用super.resume()?
从此stacktrace行android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
答案 3 :(得分:0)
请参阅this类似问题,该问题描述了您所看到的相同行为,但解决方案可能不适用于您的情况。要解决您的问题,您只需检查代码在onCreateView
中依赖的任何内容是null
。