每当我加载了一个片段的主要活动并且用户启动一个新活动、切换设备的方向并返回到主要活动时,我都会收到此错误。
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.home_layout);
super.onCreate(savedInstanceState);
fragmentManager = getSupportFragmentManager();
fragment = fragmentManager.findFragmentById(R.id.layFragment);
initialize();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.home_layout);
initialize();
super.onConfigurationChanged(newConfig);
}
private void initialize() {
layStatus = (LinearLayout) findViewById(R.id.layStatus);
txtStatus = (TextView) findViewById(R.id.txtStatus);
....
handleFragments(lastFragmentId);
}
public void handleFragments(int fragmentId) {
if (fragment == null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (fragmentId==someFragmentId){
ft.replace(R.id.layFragment, new FragmentSomeFragment());
}
else
....
ft.commit();
}
}
在我的 android 清单中,活动声明为:
<activity
android:name=".HomeActivity"
android:configChanges="keyboardHidden|orientation" />
<activity
在关于 SO 的另一个问题中,我发现这可能是由 Support 库中的一个错误引起的,我是否添加了任何运气:
// needed as a workaround for a bug in the Support library
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
我的应用程序从 android 2.2 运行,并且我正在使用 android-support-v4.jar 支持库来获取片段。
日志如下所示:
07-27 11:56:20.399: E/AndroidRuntime(16021): FATAL EXCEPTION: main
07-27 11:56:20.399: E/AndroidRuntime(16021): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
07-27 11:56:20.399: E/AndroidRuntime(16021): at com.rightcab.driver.core.HomeActivity.handleFragments(HomeActivity.java:341)
07-27 11:56:20.399: E/AndroidRuntime(16021): at com.rightcab.driver.core.HomeActivity.initialize(HomeActivity.java:128)
07-27 11:56:20.399: E/AndroidRuntime(16021): at com.rightcab.driver.core.HomeActivity.onConfigurationChanged(HomeActivity.java:153)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3618)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:3771)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.os.Looper.loop(Looper.java:137)
07-27 11:56:20.399: E/AndroidRuntime(16021): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-27 11:56:20.399: E/AndroidRuntime(16021): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:56:20.399: E/AndroidRuntime(16021): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 11:56:20.399: E/AndroidRuntime(16021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 11:56:20.399: E/AndroidRuntime(16021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 11:56:20.399: E/AndroidRuntime(16021): at dalvik.system.NativeStart.main(Native Method)