【问题标题】:View binding does not have access to fragment视图绑定无权访问片段
【发布时间】:2021-06-01 14:09:27
【问题描述】:

我有一个带有ConstraintLayout 和片段的活动:

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

  ...other views...

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

  binding = ActivityMainBinding.inflate(layoutInflater)

这里的问题是我无法访问binding.containerView。 如果是fragment 我应该使用findViewById 吗?

【问题讨论】:

    标签: android android-fragments android-viewbinding


    【解决方案1】:

    片段标签(&lt;fragment&gt;)不是view,它充当其他视图的container,所以你不能像其他视图一样访问它...

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

    你可以使用,

    supportFragmentManager.findFragmentById(R.id.containerView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2014-08-30
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多