android - 在我的 Activity 中动态添加一个 fragment

标签 android android-fragments android-framelayout

我正在使用 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/

相关文章:

android - 当应用程序处于后台时在android中修改firebase推送通知

Android Fabric - 空指针异常 : button in popup window layout

android - 框架布局 : is it possible to put child layout off-screen?

android - FrameLayout 中的 ScrollView 的问题

android - 添加 View 后刷新 LinearLayout

Android:是否可以使用 ContentObserver 只监听插入和更新操作

android - 无法从 Android 应用程序调用 Java Webservice

android - FindViewbyID 与 fragment

java - 带 ExpandableList 的 PullToRefresh 布局

android - 如何在 Activity View 中重绘 Canvas