簡體   English   中英

LiveData(MutableLiveData) 和數據綁定上升錯誤(無法調用觀察者方法)

[英]LiveData(MutableLiveData) and Databinding rise error (Failed to call observer method)

我有一個應用程序,它使用 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)

請幫我解決這個令人困惑的錯誤!!

您必須使用字符串值來設置android:text

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

或者你也可以簡單地這樣做

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM