我有一个名为ActivityHomeActivity,它使用片段HomeFragmentOwnershipFragment

当我单击HomeFragment中的列表视图项时,将执行片段事务并启动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的listview项。 (工作正常)
我按下“后退”按钮,这将启动一个事务,该事务返回到HomeFragment并将OwnershipFragment放入后退堆栈。 (工作正常)
我转动手机将方向更改为横向模式。 (工作正常)
我将其恢复为纵向,应用程序崩溃并在以下位置给出NullPointerException



  for(int i = 0; i

我不知道为什么会这样,因为我找不到在mDirectors分配了null值的位置。我不确定要提供什么其他代码,所以请告诉我,我将其放入。

如果显示的当前片段是onSaveInstanceState,为什么要首先调用OwnershipFragmentHomeFragment

这是我的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)

最佳答案

根据您的评论,将mDirectors的定义更改为

ArrayList<Director> mDirectors = new ArrayList<Director>();

或检查您的as中是否为null

for(int i = 0; mDirectors != null && i < mDirectors.size(); i++)

10-04 19:27