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

时间:2018-05-20 10:03:01

标签: 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);
    }

我的模特课:

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:无法调用observer方法---------   堆栈跟踪---------

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)

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

2 个答案:

答案 0 :(得分:3)

您必须为集合android:text使用字符串值

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

答案 1 :(得分:-1)

或者您也可以简单地做到这一点


describe('RxjsUtilsService', () => {
  let rxJSUtilsService: RxJSUtilsService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [RxJSUtilsService],
    }).compile();

    rxJSUtilsService = module.get<RxJSUtilsService>(RxJSUtilsService);
  });

  it('should be defined', () => {
    expect(rxJSUtilsService).toBeDefined();
  });

  it('GenericRetryStrategy should be called', () => {
    const source = interval(100);
    jest.spyOn(rxJSUtilsService, 'genericRetryStrategy');
    source
      .pipe(
        mergeMap(val => {
          return val < 2 ? throwError('divisible by six') : of(val);
        }),
        retryWhen(
          rxJSUtilsService.genericRetryStrategy({
            maxRetryAttempts: 10,
            scalingDuration: 1000,
          }),
        ),
      )
      .subscribe({
        next: val => {
          expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
        },
      });
  });

  it('GenericRetryStrategy should throw error', () => {
    const source = interval(100);
    jest.spyOn(rxJSUtilsService, 'genericRetryStrategy');
    source
      .pipe(
        mergeMap(val => {
          return val > 0 ? throwError('divisible by six') : of(val);
        }),
        retryWhen(
          rxJSUtilsService.genericRetryStrategy({
            maxRetryAttempts: 2,
            scalingDuration: 1000,
          }),
        ),
      )
      .subscribe({
        error: err => {
          expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
        },
      });
  });
});