gpt4 book ai didi

android - View 绑定(bind)无权访问 fragment

转载 作者:行者123 更新时间:2023-12-04 16:24:39 25 4
gpt4 key购买 nike

我有一个 Activity ConstraintLayout并在其上分段:

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

...other views...
我在 Activity 代码中使用 View 绑定(bind):
  binding = ActivityMainBinding.inflate(layoutInflater)
这里的问题是我无法访问 binding.containerView .
如果是 fragment我应该使用 findViewById ?

最佳答案

fragment 标签 ( <fragment> ) 不是 view它充当 container对于其他 View ,因此您无法像其他 View 一样访问它...
当您尝试使用 binding 访问非 View 元素时会出现编译错误
相反,您可以使用,

supportFragmentManager.findFragmentById(R.id.containerView)

关于android - View 绑定(bind)无权访问 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67790410/

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