Android 3.6中的ViewStubProxy未解决的引用问题

6

I have this Kotlin code:

if(this.fragmentMeasurementBinding.viewStub.isInflated)
{
    return;
}
this.fragmentMeasurementBinding.viewStub.viewStub?.layoutResource =
      R.layout.layout_measurement_single_value
this.fragmentMeasurementBinding.viewStub.setOnInflateListener { _, inflated ->
      LayoutMeasurementSingleValueBinding.bind(inflated)?.let {
          it.textInputLayoutSingleMeasurementValue.hint = Helper.getDynamicStringResource(
            this.context, parent.getItemAtPosition(position).toString(), prefix = "title_")
      }
}
this.fragmentMeasurementBinding.viewStub.viewStub?.inflate() 

我的XML文件中有这样的内容:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.MeasurementFragment">

    <ViewStub
        android:id="@+id/viewStub"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_input_layout_measure" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/text_input_layout_measure"
        style="@style/AppTheme.Widget.TextInput.ExposedDropdownMenu"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:descendantFocusability="blocksDescendants"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <AutoCompleteTextView
            android:id="@+id/exposed_dropdown_measure"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/what_are_you_measure"
            android:imeOptions="actionNext"
            android:labelFor="@id/text_input_layout_measure" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout> 

当我编译 Android Studio 时,会出现以下错误报告:
  • 未解决的引用:isInflated
  • 未解决的引用:viewStub
  • 未解决的引用:viewStub
我正在使用来自 Google 的新 ViewBinding,并使用 Android Studio 3.6 Canary 12、Gradle 3.6.0-alpha12 进行开发。 这是 Android Studio 的 bug 还是我的 bug?

我在使用简单视图绑定和Android Studio 3.6.1时遇到了相同的问题。 - Josttie
听起来像是一个 bug,你可能想要报告 - Harvey
你解决了这个问题吗?我现在也遇到了这个问题,但只是在我的项目中的某些文件中。 - Mark Herscher
有关这个的任何消息吗?我也很感兴趣。 - Guilherme Santos
如果这个问题得到解决,我也希望得到更新。 - Dannie
2个回答

0

我在任何地方都使用 ViewBinding,然而,由于这个 ViewStubProxy 未解决的引用 的问题,我无法找到解决办法,在这种特殊情况下,我使用了好朋友 findViewById

例如:

class MainActivity: AppCompatActivity{

    //...

    private val myView: View by lazy {
        findViewById<ViewStub>(R.id.my_view_stub).inflate()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        //...
    }

    //...

    fun displayMyView() {
        myView.isVisible = true
        //
    }

}

或者,如果您需要使用绑定来处理充气桩的内部视图:

private val myViewBinding: MyViewBinding by lazy {
    MyViewBinding.bind(findViewById<ViewStub>(R.id.my_view_stub).inflate())
}

0

我曾经遇到过同样的问题。

我检查了BindingClass的实际生成方式。 生成的类位于build/generated/databinding base_class_source_out文件夹中。

导致问题的ViewStubProxy实际上是在生成的类中作为ViewStub实现的。 当我将布局xml和目标布局xml转换为数据绑定布局后,问题就解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接