10

我有一个使用 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);
    }

还有我的模特课:

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);
    }    
}

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

<?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>

在运行时和创建活动时,我收到此错误:

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)

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

4

2 回答 2

38

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

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@{String.valueOf(viewmodel.student.id)}" />
于 2018-08-12T14:03:55.907 回答
-3

或者你也可以简单地这样做

       <TextView 
                 android:text="@{viewmodel.student.id.toString}" />   
       <TextView 
                 android:text="@{viewmodel.student.family.toString}" />    

       <TextView
                 android:text="@{viewmodel.student.id.toString}"/> 
于 2019-11-27T18:24:31.050 回答