这很奇怪。我有一个简单的应用程序,一旦登录,它就会在活动中显示一个片段。该应用程序还有一个不活动“超时”,在此之后它完成活动并显示登录屏幕 - 如果应用程序在超时发生时处于后台,那么当活动中发生下一个onCreate
或onStart
事件时它就完成了。
但是,有时当返回登录的活动时,我会在我的片段的 onActivityCreated 方法中得到一个 NPE。片段非常简单,只需调用字符串上的方法。字符串本身是通过调用 getArguments() 来检索的。我已经验证,每当创建 Fragment 时,总是会设置参数。
因此,在片段中似乎以某种方式onActivityCreated
实际上调用onCreate
了它,我知道这应该是不可能的。以下是堆栈跟踪的一部分:
01-14 15:34:37.176: E/AndroidRuntime(3272): at com.example.app.fragment.MyFragment.onActivityCreated(MyFragment.java:203)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(FragmentManager.java:1888)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentActivity.onReallyStop(FragmentActivity.java:787)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentActivity.doReallyStop(FragmentActivity.java:764)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:322)
01-14 15:34:37.176: E/AndroidRuntime(3272): at com.actionbarsherlock.app.SherlockFragmentActivity.onDestroy(SherlockFragmentActivity.java:261)
01-14 15:34:37.176: E/AndroidRuntime(3272): at com.example.app.MyActivity.onDestroy(MyActivity.java:195)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.app.Activity.performDestroy(Activity.java:5273)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
01-14 15:34:37.176: E/AndroidRuntime(3272): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3562)