【问题标题】:Android 3.6 ViewStubProxy Unresolved referenceAndroid 3.6 ViewStubProxy 未解析参考
【发布时间】:2019-10-02 08:55:38
【问题描述】:

我有这个 Kotlin 代码:

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 的错误还是我的错误?

【问题讨论】:

  • 我对简单的视图绑定和Android studio 3.6.1有同样的看法
  • 听起来像一个错误you may want to report
  • 好奇你是否曾经解决过这个问题。我现在遇到了这个问题,但仅限于我项目中的一些文件。
  • 有这方面的消息吗?我也有兴趣。
  • 如果这个问题得到修复,我也想要更新

标签: android android-studio kotlin android-gradle-plugin android-viewbinding


【解决方案1】:

我在任何地方都使用ViewBinding,但是,由于ViewStubProxy Unresolved reference 的这个问题,我也找不到解决方案,在这种特殊情况下,我使用好朋友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())
}

【讨论】:

    【解决方案2】:

    我有同样的问题。

    我检查了 BindingClass 是如何实际生成的。 生成的类在 build/generated/databinding base_class_source_out 文件夹中。

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

    【讨论】:

      猜你喜欢
      • 2022-10-02
      • 2020-06-12
      • 2023-04-05
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多