“添加到FragmentContainerView的视图必须与Fragment关联".与android Nav组件 [英] "Views added to a FragmentContainerView must be associated with a Fragment" with android Nav Component

查看:367
本文介绍了“添加到FragmentContainerView的视图必须与Fragment关联".与android Nav组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当导航组件切换到片段时,我收到此消息:添加到FragmentContainerView的视图必须与片段相关联"崩溃.是什么原因造成的?

When nav component switches to a fragment, I get this "Views added to a FragmentContainerView must be associated with a Fragment" crash. What causes this?

推荐答案

我在任何地方都没有看到此内容,因此花了一些时间才弄清楚,但在这种情况下,我试图在迁移时设置旧的旧片段导航拱组件.

I didn't see this mentioned anywhere and it took a while to figure out but in this case, I was trying to set up a old legacy fragment while migrating to the nav arch component.

原因是在碎片的 onCreateView 中,膨胀看起来像:

The reason was in the frag's onCreateView, the inflate looked like:

layoutView = inflater.inflate( R.layout.home, container, true );

最后一个参数自动将视图附加到容器.这在旧样式的片段和活动中效果很好.它不适用于nav arch组件,因为根容器是 FragmentContainerView ,它只允许将片段附加到其上.

The last argument automatically attaches the view to the container. This works fine in old style fragments and activities. It does not work with the nav arch component because the root container is a FragmentContainerView which only allows fragments to be attached to it.

将最后一个参数设置为false可以使其正常工作.

Setting the last argument to false makes it work properly.

这篇关于“添加到FragmentContainerView的视图必须与Fragment关联".与android Nav组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆