LiveData(MutableLiveData) and Databinding rise error (Failed to call observer method)
Asked Answered
A

2

15

I have an app that uses ViewModel and MutableLiveData to bind live data to my UI. After hours westing my time! and review all sample on the internet I couldn't find the reason for the problem.

My activity:

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

And my model class:

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

And layout(I'vd cleaned extra codes here):

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

On runtime and on creating activity I get this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{googlearchitecturecomponents.ferdos.com.dagger211/googlearchitecturecomponents.ferdos.com.dagger211.detail.DetailActivity}: java.lang.RuntimeException: Failed to call observer method--------- Stack trace ---------

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)

Please help me with this confusing error!!

Angelus answered 20/5, 2018 at 10:3 Comment(6)
@Enzokie it causes rise an error in building level: Error:(21, 69) error: cannot find symbol class ActivityStudentBinding. Because value is not needed hereAngelus
If you think of it viewmodel.student returns MutableLiveData<StudentData.Student> which means you can't do viewmodel.student.id so I assume viewmodel.student.value.id should work (not tested).Pressor
Btw your <TextView's> does not have a container layout ;)Pressor
@Pressor As I noted it in my question: I've cleaned extra codes here ;)Angelus
Don't remove crucial part of your code because other people here will mark this question [Off-topic for Typographical] reason.Pressor
No! nothing as Caused by in logcatAngelus
S
45

You must use string value to setting android: text

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@{String.valueOf(viewmodel.student.id)}" />
Subscription answered 12/8, 2018 at 14:3 Comment(3)
Wonderful. That was the answer.Elide
God this is really helpful. Thank you!Hemocyte
Crazy error, I guess it jumps whenever a viewDataBinding method() is used and it does not even need to be related to the providing of the data that will be bind in itself, as it can even be called with a bind.setLifecycleOwner(); .... This error makes it extremely adverse to work with <imports/> for data conversion in the XML.Infamous
D
-3

Or you can simply do this also

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

       <TextView
                 android:text="@{viewmodel.student.id.toString}"/> 
Dola answered 27/11, 2019 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.