视图绑定无权访问片段 [英] View binding does not have access to fragment
本文介绍了视图绑定无权访问片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有 ConstraintLayout
和片段的活动:
I have an activity with ConstraintLayout
and fragment on it:
<androidx.constraintlayout.widget.ConstraintLayout
...attributes....
>
<fragment
android:id="@+id/containerView"
app:navGraph="@navigation/some_navigation"
...other attributes...
/>
...other views...
我在活动代码中使用视图绑定:
I'm using view binding in the activity code:
binding = ActivityMainBinding.inflate(layoutInflater)
这里的问题是我无法访问 binding.containerView
.在 fragment
的情况下,我应该使用 findViewById
吗?
the issue here that i have no access to the binding.containerView
.
Does in case of fragment
I should use findViewById
?
推荐答案
Fragment tag (
) is not a view
它充当 容器
用于其他视图,因此您无法像其他视图一样访问它...
Fragment tag ( <fragment>
) is not a view
it act as a container
for other views, So you cannot access it like other views...
当您尝试使用 binding
相反,您可以使用,
supportFragmentManager.findFragmentById(R.id.containerView)
这篇关于视图绑定无权访问片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文