我有一個Activity
調用HomeActivity
,它使用片段HomeFragment
和OwnershipFragment
。退出片段後拋出onSaveInstanceState的NullPointerException
當我單擊HomeFragment
中的listview項時,將執行片段事務並啓動OwnershipFragment
。
在OwnershipFragment
我有以下onSaveInstanceState
方法:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
for(int i = 0; i <mDirectors.size(); i++)
mDirectors.get(i).updateFromForm();
outState.putSerializable(DIRECTORS, mDirectors);
}
而且在onCreateView
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_ownership, container, false);
mFormContainer = (LinearLayout) rootView.findViewById(R.id.ownership_form_container);
mManager = new SessionManager(getActivity());
mDirectors = new ArrayList<Director>();
if (savedInstanceState != null)
mDirectors = (ArrayList<Director>) savedInstanceState.getSerializable(DIRECTORS);
else
mDirectors = mManager.getDirectors();
//Other stuff
return rootView;
}
基本上,應用程序崩潰後執行以下操作:
- 我點擊在打開
OwnershipFragment
的列表視圖項上。 (正常工作) - 我按下後退按鈕,後退按鈕開始一個事務,返回到
HomeFragment
並將OwnershipFragment
放在後退堆棧上。 (正常工作) - 我打開手機將方向更改爲橫向模式。 (正常工作)
- 我把它回縱向和應用程序崩潰,並給出了在
NullPointerException
:
對(INT I = 0;我< mDirectors.size();我++)//在方法的onSaveInstanceState
爲什麼,但因爲我找不到在哪裏mDirectors
被分配一個null
值我想不通。我不知道還有什麼其他的代碼來提供,所以才讓我知道,我把它英寸
爲什麼叫擺在首位的OwnershipFragment
onSaveInstanceState
如果顯示的當前片段是HomeFragment
?
這裏是我的logcat:
07-15 11:28:24.440: E/AndroidRuntime(12150): FATAL EXCEPTION: main
07-15 11:28:24.440: E/AndroidRuntime(12150): java.lang.NullPointerException
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.opendoors.core.OwnershipFragment.onSaveInstanceState(OwnershipFragment.java:204)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1598)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1655)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Activity.onSaveInstanceState(Activity.java:1233)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.opendoors.core.HomeActivity.onSaveInstanceState(HomeActivity.java:220)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Activity.performSaveInstanceState(Activity.java:1181)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1233)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3778)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.access$800(ActivityThread.java:152)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.os.Looper.loop(Looper.java:137)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-15 11:28:24.440: E/AndroidRuntime(12150): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 11:28:24.440: E/AndroidRuntime(12150): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-15 11:28:24.440: E/AndroidRuntime(12150): at dalvik.system.NativeStart.main(Native Method)
其中一個方法返回null if(savedIns tanceState!= null) mDirectors =(ArrayList)savedInstanceState.getSerializable(DIRECTORS); else mDirectors = mManager.getDirectors();' –
我在這些語句之前放置了一個斷點,並且在錯誤發生時它們沒有被調用。當我改變'HomeFragment'的方向時,我不認爲'OwnershipFragment'的onCreateView'甚至會被調用。 – ujvl