kotlin 中的数据绑定 TypeConverter 错误
Databinding TypeConverter error in kotlin
我在一些数据绑定中使用了 TypeConverter。问题是它需要静态函数,当我将它转换为 Kotlin 时,它会进入伴随对象,数据绑定处理器无法跟踪更改。
我收到以下错误:
java.lang.IllegalStateException: Required DataBindingComponent is null
in class ListMainBinding. A BindingAdapter in
com.noisyninja.androidlistpoc.model.DataConverter.Companion is not
static and requires an object to use, retrieved from the
DataBindingComponent. If you don't use an inflation method taking a
DataBindingComponent, use DataBindingUtil.setDefaultComponent or make
all BindingAdapter methods static.
at android.databinding.ViewDataBinding.ensureBindingComponentIsNotNull(ViewDataBinding.java:554)
如何让它识别伴随对象中的静态 @TypeConverter 注释方法
你可以在Binding Adapter方法上面加上@jvmStatic注解,像这样:
@BindingAdapter(value = "visiblity")
@JvmStatic
fun showHide(view : View, show : Boolean){
view.visibility = when {
show -> View.VISIBLE
else -> View.GONE
}
}
我在一些数据绑定中使用了 TypeConverter。问题是它需要静态函数,当我将它转换为 Kotlin 时,它会进入伴随对象,数据绑定处理器无法跟踪更改。
我收到以下错误:
java.lang.IllegalStateException: Required DataBindingComponent is null in class ListMainBinding. A BindingAdapter in com.noisyninja.androidlistpoc.model.DataConverter.Companion is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static. at android.databinding.ViewDataBinding.ensureBindingComponentIsNotNull(ViewDataBinding.java:554)
如何让它识别伴随对象中的静态 @TypeConverter 注释方法
你可以在Binding Adapter方法上面加上@jvmStatic注解,像这样:
@BindingAdapter(value = "visiblity")
@JvmStatic
fun showHide(view : View, show : Boolean){
view.visibility = when {
show -> View.VISIBLE
else -> View.GONE
}
}