45

我需要用另一个替换Fragment一个,下面是层文件:ActivityFragmentActivity

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/home_layout_container">

    <fragment android:name="com.foo.FragA"
        android:id="@+id/home_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    </FrameLayout>

所以默认情况下有FragA,现在我想在活动中用FragB替换它,我做了:

public void onRegionClicked(Region region) {
    RegionInfoFragment rif = RegionInfoFragment.newInstance(region);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.home_list, rif);
}

但我有例外:

>6:24:40.685: ERROR/AndroidRuntime(9194): Uncaught handler: thread main exiting due to uncaught exception
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1857)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.view.ViewGroup.addView(ViewGroup.java:1752)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.view.ViewGroup.addView(ViewGroup.java:1709)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.view.ViewGroup.addView(ViewGroup.java:1689)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:578)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1219)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:380)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.os.Handler.handleCallback(Handler.java:587)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.os.Looper.loop(Looper.java:123)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at java.lang.reflect.Method.invoke(Method.java:521)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194):     at dalvik.system.NativeStart.main(Native Method)

我能做些什么?

谢谢!

4

3 回答 3

119

您不能替换布局文件中静态定义的片段。您只能替换通过FragmentTransaction.

于 2011-05-06T06:38:46.617 回答
5

尝试commit()在 ft 结束时,您需要对其进行commit()更改以使其知道。

于 2011-12-20T07:24:03.127 回答
1
getFragmentManger()
  .beginTransaction
  .replace(R.id.frame,Yourfragment.newInstance(),null)
  .addtobackstack
  .commit();

人们总是说我是单线的,所以这里有一个适合你的单线解决方案

于 2019-09-25T12:35:52.740 回答