在我的 Activity 上动态添加片段

发布于 2025-01-02 00:14:52 字数 550 浏览 2 评论 0原文

我正在使用片段,当我在左侧片段上选择某些内容时,我希望我的主要活动创建一个右侧片段。

所以我这样做是为了在 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 视图吗?还是另一种看法?怎么办呢?

I'm using fragments and when I select something on my left fragment, I want a right fragment to be created by my main activity.

So I did this to add my Fragment on a FrameLayout :

//add a fragment
FluxDetailFragment myFragment = new FluxDetailFragment();
fragmentTransaction.add(R.id.frameLayout3, myFragment);
fragmentTransaction.commit();

But I get this error :

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.

Do I have to remove my FrameLayout view before? Or another view? How to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-09 00:14:52

为了解决我的问题,我这样做了:

View result = inflater.inflate(R.layout.view2);

相反,我这样做了:

View result = inflater.inflate(R.layout.view2, null);

attachToRoot 必须设置为 null 才能工作。

To solve my problem, I had this :

View result = inflater.inflate(R.layout.view2);

Instead of that, I did this :

View result = inflater.inflate(R.layout.view2, null);

attachToRoot must be set to null to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文