我需要将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>所以默认情况下有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 exception05-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)我能做什么?
4 回答
米脂
TA贡献1836条经验 获得超3个赞
getFragmentManger()
.beginTransaction
.replace(R.id.frame,Yourfragment.newInstance(),null)
.addtobackstack
.commit();
人们总是说我是一线客,所以这是您的一线客解决方案
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
将一个片段替换为另一个片段,并在后堆栈中保留先前的状态:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
- 4 回答
- 0 关注
- 503 浏览
添加回答
举报
0/150
提交
取消