我正在尝试学习如何Fragment
在android中使用s.当我在Android中调用fragment
new时,我正试图删除旧的fragment
.
我需要替换Fragment
一个Activity
与另一个Fragment
,下面是层文件Activity
:
<?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>
Run Code Online (Sandbox Code Playgroud)
所以默认情况下有FragA,现在我想在活动中用FragB替换它,我做了:
public void onRegionClicked(Region region) {
RegionInfoFragment rif = RegionInfoFragment.newInstance(region);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.home_list, rif);
}
Run Code Online (Sandbox Code Playgroud)
但我得到了例外:
>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): …
Run Code Online (Sandbox Code Playgroud)