找不到属性'android:text'的配置器 - Android MVVM



我在android MVVM架构中经常收到此错误。我尝试删除.idea,igradle,gradle文件夹,但它不起作用。我什至尝试使用无效缓存重新启动工作室,这也不起作用。

下面是示例代码

public class TestViewModel extends AndroidViewModel {
MutableLiveData<String> test = new MutableLiveData<>();
public TestViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<String> getTest() {
return test;
}
public void setTest(MutableLiveData<String> test) {
this.test = test;
}
}

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="in.raji.bills.billsreminder.viewmodels.TestViewModel"
/>
<variable
name="viewModel"
type="in.raji.bills.billsreminder.viewmodels.TestViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/relativeLayout8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".MonthlyFragment">
<EditText
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="Enter title"
android:text="@{viewModel.test}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</layout>

解决方案

LiveData 可以用作 Android Studio 3.1 中的可观察字段。這是舊 Android Studio 的錯誤,請更新您的 Android Studio。查看发行说明。

建议

  • 删除<import type="in.raji.bills.billsreminder.viewmodels.TestViewModel"/>
  • 您可以将MutableLiveData<String> test设为私有。

最新更新