1

什么可能导致此异常?

E/AndroidRuntime(16901): FATAL EXCEPTION: main
E/AndroidRuntime(16901): Process: com.borqs.karbonn.music, PID: 16901
E/AndroidRuntime(16901): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E/AndroidRuntime(16901):在 android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
E/AndroidRuntime(16901):在 android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
E/AndroidRuntime(16901 ) ): 在 android.app.BackStackRecord.commitInternal(BackStackRecord.java:609)
E/AndroidRuntime(16901): 在 android.app.BackStackRecord.commit(BackStackRecord.java:587)
E/AndroidRuntime(16901):在 com.borqs.music.MusicHubMainActivity.onNavigationItemSelected(MusicHubMainActivity.java:1167)
E/AndroidRuntime(16901):在 com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:148) )
E/AndroidRuntime(16901): 在 android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
E/AndroidRuntime(16901): 在 android.widget.AdapterView.access$200(AdapterView.java:48)
E/AndroidRuntime( 16901): 在 android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
E/AndroidRuntime(16901): 在 android.os.Handler.handleCallback(Handler.java:808)
E/AndroidRuntime(16901): 在android.os.Handler.dispatchMessage(Handler.java:103)
E/AndroidRuntime(16901): 在 android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(16901): 在 android.app.ActivityThread.main(ActivityThread.java:5299)
E/AndroidRuntime(16901) : 在 java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16901): 在 java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(16901): 在 com.android .internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
E/AndroidRuntime(16901): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)^ME
/AndroidRuntime(16901 ) ): 在 dalvik.system.NativeStart.main(Native Method)

这是我的 onSave 实例代码

@Override
public void onSaveInstanceState(Bundle state)
{
    super.onSaveInstanceState(state);
    saveInstance =true;
    if (state == null)
    {
        FragmentManager mFragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        MediaPlaybackFragment fragment = new MediaPlaybackFragment();
        fragmentTransaction.add(R.id.content_frame, fragment);
        fragmentTransaction.commit();
    } else {
        state.putInt("position", mPostion);
        selectedTab = tabHost.getCurrentTab();
        state.putInt("tabhostselected", selectedTab);
        state.putBoolean("isSaveInstance", saveInstance);
        // state.putBoolean("draweropened", mDrawergarment.isDrawerOpened());
    }
}

这是我的导航代码

@Override
public boolean onNavigationItemSelected(int position, long itemId) {
    Fragment newFragment = null;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    newFragment = new MediaPlaybackFragment();
    ft.replace(R.id.content_frame, newFragment, "Music");
    ft.commit();
}

Any help would be much appreciated, if you need more information please ask.
4

1 回答 1

2

改变

fragmentTransaction.commit();

fragmentTransaction.commitAllowingStateLoss();

可能您在代码中的其他地方提交事务 - 您也应该在那里使用 commitAllowingStateLoss()。但我建议您重写代码以避免在 onSaveInstanceState() 之后提交

于 2014-10-29T08:14:57.273 回答