我刚刚开始使用Fragments,我正在尝试动态添加/替换片段。
我可以添加它们就好了,就像这样......
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FragmentManager fragMan = getSupportFragmentManager();
FragmentTransaction fragTran = fragMan.beginTransaction();
FragOne fOne = new FragOne();
FragTwo fTwo = new FragTwo();
FragThree fThree = new FragThree();
fragTran.add(R.id.frag_cont_one, fOne);
fragTran.add(R.id.frag_cont_two, fTwo);
fragTran.commit();
}
但是当我尝试用fThree替换fTwo时,应用程序崩溃了。以下是我如何进行替换
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();
有什么想法吗?
由于
10-08 21:30:44.710: E/Trace(3219): error opening trace file: No such file or directory (2)
10-08 21:30:44.869: D/AndroidRuntime(3219): Shutting down VM
10-08 21:30:44.869: W/dalvikvm(3219): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-08 21:30:44.889: E/AndroidRuntime(3219): FATAL EXCEPTION: main
10-08 21:30:44.889: E/AndroidRuntime(3219): java.lang.RuntimeException: Unable to start activity ComponentInfo{evans.louis.fragments/evans.louis.fragments.Home}: java.lang.IllegalStateException: commit already called
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.os.Looper.loop(Looper.java:137)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-08 21:30:44.889: E/AndroidRuntime(3219): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 21:30:44.889: E/AndroidRuntime(3219): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 21:30:44.889: E/AndroidRuntime(3219): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-08 21:30:44.889: E/AndroidRuntime(3219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-08 21:30:44.889: E/AndroidRuntime(3219): at dalvik.system.NativeStart.main(Native Method)
10-08 21:30:44.889: E/AndroidRuntime(3219): Caused by: java.lang.IllegalStateException: commit already called
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
10-08 21:30:44.889: E/AndroidRuntime(3219): at evans.louis.fragments.Home.onCreate(Home.java:37)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.Activity.performCreate(Activity.java:5104)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-08 21:30:44.889: E/AndroidRuntime(3219): ... 11 more
编辑:添加了LogCat
答案 0 :(得分:20)
问题出在你的logcat输出中:
Caused by: java.lang.IllegalStateException: commit already called
您需要开始新交易:
fragTran = fragMan.beginTransaction();
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();
答案 1 :(得分:0)
我解决了这个问题
在调用commit()
并再次替换片段后,您应该从
fragmentTransaction = fragmentManager.beginTransaction();
if(position==0){
fragmentTransaction.replace(android.R.id.content, f1_fragment);
else{
fragmentTransaction.replace(android.R.id.content, f2_fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
否则你将得到已经被称为错误的提交