LiveData(MutableLiveData)和数据绑定上升错误(无法调用观察器方法)

Sia*_*dos 4 android viewmodel android-viewholder android-livedata mutablelivedata

我有一个使用ViewModel和MutableLiveData将实时数据绑定到我的UI的应用程序。经过几个小时的时间!并在互联网上查看所有样本,但我找不到问题的原因。

我的活动:

public class DetailActivity extends DaggerAppCompatActivity {
    ActivityStudentBinding mViewDataBinding;    
    MyModel myModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_student);
        mViewDataBinding.setLifecycleOwner(this);
        myModel = ViewModelProviders.of(this).get(MyModel.class);
        mViewDataBinding.setViewmodel(myModel);
    }
Run Code Online (Sandbox Code Playgroud)

我的模型课:

public class MyModel extends ViewModel
{
    public MutableLiveData<StudentData.Student> student = new MutableLiveData<>();

    public MyModel() {
        this.student=student;
        StudentData.Student student = new StudentData().getFirstStudent();
        this.student.setValue(student);
    }    
}
Run Code Online (Sandbox Code Playgroud)

和布局(我在这里清理了多余的代码):

<?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 >
        <variable
             name="viewmodel"
            type="googlearchitecturecomponents.ferdos.com.dagger211.detail.MyModel"/>
    </data>
        <TextView               
            android:text="@{viewmodel.student.id}" />    
        <TextView
            android:text="@{viewmodel.student.family}" />    
        <TextView
            android:text="@{viewmodel.student.id}"/>    
</layout>
Run Code Online (Sandbox Code Playgroud)

在运行时和创建活动时,出现以下错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {googlearchitecturecomponents.ferdos.com.dagger211 / googlearchitecturecomponents.ferdos.com.dagger211.detail.DetailActivity}:java.lang.RuntimeException:无法调用观察者方法------ - - 堆栈跟踪 - - - - -

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

android.app.ActivityThread.access$800(ActivityThread.java:144)

android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)

android.os.Handler.dispatchMessage(Handler.java:102)

android.os.Looper.loop(Looper.java:135)

android.app.ActivityThread.main(ActivityThread.java:5221)

java.lang.reflect.Method.invoke(Native Method)

java.lang.reflect.Method.invoke(Method.java:372)

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Run Code Online (Sandbox Code Playgroud)

请帮助我解决这个令人困惑的错误!

Ahm*_*deh 13

您必须使用字符串值进行设置 android:text

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@{String.valueOf(viewmodel.student.id)}" />
Run Code Online (Sandbox Code Playgroud)