视图绑定无权访问片段



我有一个ConstraintLayout的活动,上面有片段:

<androidx.constraintlayout.widget.ConstraintLayout 
...attributes....
>
<fragment
android:id="@+id/containerView"
app:navGraph="@navigation/some_navigation"
...other attributes...
/>
...other views...

我在活动代码中使用视图绑定:

binding = ActivityMainBinding.inflate(layoutInflater)

这里的问题是我无法访问CCD_ 2。如果是fragment,我应该使用findViewById吗?

Fragment标记(<fragment>(不是view,它充当其他视图的container,因此您无法像访问其他视图一样访问它。。。

当您尝试使用binding访问非视图元素时,会出现编译错误

相反,您可以使用

supportFragmentManager.findFragmentById(R.id.containerView)

最新更新