我正在使用 fragment ,当我在左侧 fragment 上选择某些内容时,我希望通过我的主要 Activity 创建一个右侧 fragment 。
所以我这样做是为了在 FrameLayout 上添加我的 fragment :
//add a fragment
FluxDetailFragment myFragment = new FluxDetailFragment();
fragmentTransaction.add(R.id.frameLayout3, myFragment);
fragmentTransaction.commit();
但是我得到这个错误:
02-02 15:34:03.104: E/AndroidRuntime(14794): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我必须先删除我的 FrameLayout View 吗?还是另一种观点?怎么做?
最佳答案
为了解决我的问题,我有这个:
View result = inflater.inflate(R.layout.view2);
取而代之的是,我这样做了:
View result = inflater.inflate(R.layout.view2, null);
attachToRoot 必须设置为 null 才能工作。
关于android - 在我的 Activity 中动态添加一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114143/