java – 动态替换Android片段

前端之家收集整理的这篇文章主要介绍了java – 动态替换Android片段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始使用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添加

解决方法

问题出在你的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();

猜你在找的Android相关文章