Android Studio 3.4 Kotlin BindingAdapter 使 ap 崩溃

Android Studio 3.4 Kotlin BindingAdapter crashes the ap

几天来我一直在尝试在 Android Studio 3.4(最新更新)中使用 BindingAdapter 和 Kotlin,但似乎没有任何效果。

我首先尝试了以下教程:https://codelabs.developers.google.com/codelabs/android-databinding/#7 我一到第8步就报错

此外,我尝试了使用空应用程序、单个 Activity、单个 ViewModel 和单个 BindingAdapter 的简单示例。这是 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="viewmodel" 
                  type="com.example.testbindingadapter.DataViewModel"/>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:greetings="@{viewmodel.name}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" 
                android:id="@+id/textView"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

现在是带有 BindingAdapter 的 ViewModel

class DataViewModel : ViewModel() {

    private val _name = MutableLiveData<String>()
    val name : LiveData<String> = _name
    init {
        _name.value = "Amath"
    }
}

@BindingAdapter("greetings")
fun setName(view: TextView, text: String) {
    view.text = "Welcome, $text"
}

我还在我的 Graddle 中启用了 dataBinging。我按照以下线程 中的建议添加了 apply plugin: 'kotlin-kapt'。起初我有一个错误 msg:Cannot find the setter for attribute databinding 随后错误消失了,但应用程序只是崩溃了。

你能帮忙吗?

您从未将视图模型设置为数据绑定:

binding.viewmodel = viewModel