gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:22:01 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com