我有一个创建programmaticaly的片段(没有在活动布局中声明)。但是当我第二次尝试打开它时,应用程序因“提交已经调用”而崩溃。
日志:
01-11 04:14:41.469: E/AndroidRuntime(13556): FATAL EXCEPTION: main
01-11 04:14:41.469: E/AndroidRuntime(13556): java.lang.IllegalStateException: commit already called
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.app.BackStackRecord.commit(BackStackRecord.java:574)
01-11 04:14:41.469: E/AndroidRuntime(13556): at neviat.tbf.tartarugaburguer.Activity_Shopping.openFragment(Activity_Shopping.java:235)
01-11 04:14:41.469: E/AndroidRuntime(13556): at neviat.tbf.tartarugaburguer.Activity_Shopping.onOptionsItemSelected(Activity_Shopping.java:226)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.widget.AbsListView$1.run(AbsListView.java:3423)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.os.Handler.handleCallback(Handler.java:725)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.os.Looper.loop(Looper.java:137)
01-11 04:14:41.469: E/AndroidRuntime(13556): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-11 04:14:41.469: E/AndroidRuntime(13556): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 04:14:41.469: E/AndroidRuntime(13556): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 04:14:41.469: E/AndroidRuntime(13556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 04:14:41.469: E/AndroidRuntime(13556): at dalvik.system.NativeStart.main(Native Method)
在活动中 - 创建片段的功能:
private void openFragment(Fragment fragment, String tag) {
fragmentTransaction.replace(android.R.id.content, fragment, tag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
在片段中 - 关闭片段的函数:
private void finish() {
getFragmentManager().popBackStack();
}
答案 0 :(得分:3)
**
不要重复使用传入的事务,而是创建一个新事务 FragmentTransaction实例。
**
由于
答案 1 :(得分:1)
问题是您没有创建新的交易,这就是您收到此错误的原因。您必须开始新的交易:
fragTransaction = fragManager.beginTransaction();
fragTransaction.replace(R.id.old_fragment, newFragment);
fragTransaction.addToBackStack(null);
fragTransaction.commit();
感谢。