找不到属性“ android:text”的设置器

时间:2019-08-11 05:22:47

标签: android mvvm android-databinding android-livedata mutablelivedata

我有:

function ofTable(...)
  return {...}
end

具有activity.java:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <import type="android.view.View" />
        <variable name="vm" type="com.my.package.viewmodel.SplashActivityVM" />
    </data>

    <android.support.v7.widget.LinearLayoutCompat
        bla...>

        <TextView
            bla...
            android:text='@{vm.status}'
            bla... />
bla...

和我的ViewModel:

@Override
protected void onCreate(Bundle savedInstanceState) { bla... }

private void observeStatus(SplashActivityVM viewModel) {
        viewModel.getStatus().observe(this, status -> {
            if (status != null) {
                mBinding.setVm(viewModel);
            } else { }
            mBinding.executePendingBindings();
        });
    }

我收到以下错误消息:

  

msg:找不到具有参数类型的属性“ android:text”的设置器   android.arch.lifecycle.MutableLiveData   在android.widget.TextView上。

怎么了?

1 个答案:

答案 0 :(得分:0)

  1. 您需要为自己的SplashState自定义binding adapter

    TextViewAdapters.kt

@BindingAdapter("android:text")
fun setText(view: TextView, state: SplashState) {
    view.text = state.toString()
}
  1. viewLifecycleOwner设置为实时数据的数据绑定
@Override
protected void onCreate(Bundle savedInstanceState) { bla... 

mBinding.setLifecycleOwner(this);
}
  1. 删除observeStatus方法,将VM设置为绑定
@Override
protected void onCreate(Bundle savedInstanceState) { bla... 

mBinding.setLifecycleOwner(this);
mBinding.setvm(viewModel);
}