找不到属性'android:text'的setter - Android MVVM

问题描述 投票:0回答:1

我一直在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>
android android-databinding android-viewmodel
1个回答
0
投票

Solution

LiveData可用作Android Studio 3.1中的可观察字段。这是旧版Android Studio的错误,请更新您的Android Studio。 Check Release Notes

建议

  • 删除<import type="in.raji.bills.billsreminder.viewmodels.TestViewModel"/>
  • 你可以把MutableLiveData<String> test私有化。
© www.soinside.com 2019 - 2024. All rights reserved.