我有一個片段活動。片段中的旋轉器確定旋轉器下方出現2個片段中的哪一個。這2個片段繼承了一個抽象片段。在片段中,用戶在字段中輸入一些數據。我需要在屏幕旋轉後恢復數據。如何使用onSaveInstanceState
?我不想保留碎片或其他東西。還原嵌套片段旋轉
代碼
- 在活動中我不做的旋轉或節電狀態相關的任何事情。我不覆蓋
onSaveInstanceState
。 在片段與微調
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(SPINNER_SELECTION, selectedPosition); outState.putParcelable(FIELDS, getFragmentManager() .saveFragmentInstanceState(getFragmentManager() .findFragmentById(R.id.container_with_fields))); }
在
onCreateView
我檢查if (savedInstanceState == null)
和恢復領域。- 在內部碎片我什麼都不做。
錯誤
目前onSaveInstanceState
方法被調用,但不是onCreateView
方法。介於兩者之間,我得到以下錯誤,似乎什麼都沒有做我的代碼:
07-03 10:33:06.079: W/dalvikvm(4286): threadid=1: thread exiting with uncaught exception (group=0x41e26700)
07-03 10:33:06.079: E/AndroidRuntime(4286): FATAL EXCEPTION: main
07-03 10:33:06.079: E/AndroidRuntime(4286): java.lang.NullPointerException
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1599)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1655)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.Activity.onSaveInstanceState(Activity.java:1238)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.Activity.performSaveInstanceState(Activity.java:1186)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1240)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3851)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.ActivityThread.access$800(ActivityThread.java:159)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.os.Looper.loop(Looper.java:176)
07-03 10:33:06.079: E/AndroidRuntime(4286): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-03 10:33:06.079: E/AndroidRuntime(4286): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 10:33:06.079: E/AndroidRuntime(4286): at java.lang.reflect.Method.invoke(Method.java:525)
07-03 10:33:06.079: E/AndroidRuntime(4286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
07-03 10:33:06.079: E/AndroidRuntime(4286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
07-03 10:33:06.079: E/AndroidRuntime(4286): at dalvik.system.NativeStart.main(Native Method)
的應用程序不會崩潰,但活動似乎拿完。旋轉後,屏幕很快凍結,並顯示父活動。
我檢查了我有最新的SDK和libs在項目中安裝和使用。