美文网首页
java.lang.IllegalStateException:

java.lang.IllegalStateException:

作者: 因为我的心 | 来源:发表于2021-04-07 17:08 被阅读0次

一、前言:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onepiece.charging_elf/com.onepiece.charging_elf.ui.activity.BatteryCoolingActivity}: java.lang.IllegalStateException: Required DataBindingComponent is null in class ActivityBatteryyCoolingBindingImpl. A BindingAdapter in com.onepiece.charging_elf.viewmodel.BatteryCoolingViewModel.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.

意思是说DataBindingUtils这个类中的方法不是静态的,需要从DataBindingComponent中检索一个对象来使用。如果不使用带DataBindingComponent的通胀方法,请使用DataBindingUtil。setDefaultComponent或者让所有BindingAdapter方法都是静态的。

解决办法:

图片.png

1、两种办法任选其一,但是注意:必须要加@JvmStatic,不加的话还是会报刚刚错,伴随对象的成员很像其它语言中的静态成员,但在运行时它们任然是真正对象的成员实例;
2、如果你在 JVM 上使用 @JvmStatic 注解,你可以有多个伴随对象生成为真实的静态方法和属性;
3、自定义数据绑定,必须在项目的build.gradle中添加

apply plugin: 'kotlin-kapt'

注意:Kotlin最新注解变样式了

 companion object{
        @BindingAdapter("imageFromUrl")
        @kotlin.jvm.JvmStatic
        fun bindImageFromUrl(view: ImageView, imageUrl: String?) {
            if (!imageUrl.isNullOrEmpty()) {
                Glide.with(view.context)
                    .load(imageUrl)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(view)
            }
        }
    }

相关文章

网友评论

      本文标题:java.lang.IllegalStateException:

      本文链接:https://www.haomeiwen.com/subject/mbpukltx.html