栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Kotlin+使用BindingAdapter注解,报错:java.lang.IllegalStateException: Required DataBindingComponent is null

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Kotlin+使用BindingAdapter注解,报错:java.lang.IllegalStateException: Required DataBindingComponent is null

简述

java.lang.IllegalStateException: Required DataBindingComponent is null in class ActivityDatabindingbaseBindingImpl. A BindingAdapter in com.zxf.jetpackrelated.databinding.simpleUse.ImageViewAdapter 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.

简单理解一下,用BindingAdapter修饰的方法不是静态方法。但是需要是静态方法。

问题原因分析

猜测,你应该是以下两种写法之一

  1. class ImageViewAdapter{
        companion object{
            @BindingAdapter("image")
            fun setImage(imageView: ImageView, imageUrl: String?) {
                if (imageUrl.isNotNullOrEmpty()) {
                    Glide.with(imageView)
                        .load(imageUrl)
                        .into(imageView)
                } else {
                    imageView.setBackgroundColor(Color.BLUE)
                }
            }
        }
    }
    
  2. object ImageViewAdapter{
        @BindingAdapter("image")
        fun setImage(imageView: ImageView, imageUrl: String?) {
            if (imageUrl.isNotNullOrEmpty()) {
                Glide.with(imageView)
                    .load(imageUrl)
                    .into(imageView)
            } else {
                imageView.setBackgroundColor(Color.BLUE)
            }
        }
    }
    

虽然以上两种写法在使用上和静态方法差不多,但在运行时它们任然是真正对象的成员实例;可以简单看一下第一种写法生成的java代码的样子

public final class ImageViewAdapter {
   @NotNull
   public static final ImageViewAdapter.Companion Companion = new ImageViewAdapter.Companion((DefaultConstructorMarker)null);
   public static final class Companion {
      @BindingAdapter({"image"})
      public final void setImage(@NotNull ImageView imageView, @Nullable String imageUrl) {
         Intrinsics.checkNotNullParameter(imageView, "imageView");
         if (ExtensionUtilsKt.isNotNullOrEmpty((CharSequence)imageUrl)) {
            Intrinsics.checkNotNullexpressionValue(Glide.with((View)imageView).load(imageUrl).into(imageView), "Glide.with(imageView)n  …         .into(imageView)");
         } else {
            imageView.setBackgroundColor(-16776961);
         }
      }
   }
}

其实内部还是非静态的方法调用,只是通过静态字段的初始化,保证了其单例的特性。object和 这个类似,大家可以自行查看编译后的java代码。

解决

如果非要使用以上的方式,那么需要增加@JvmStatic注解,这样kotlin在编译为java代码的时候,可以为其生成对应的静态方法。

  1. class ImageViewAdapter{
        companion object{
            @BindingAdapter("image")
            @JvmStatic
            fun setImage(imageView: ImageView, imageUrl: String?) {
                if (imageUrl.isNotNullOrEmpty()) {
                    Glide.with(imageView)
                        .load(imageUrl)
                        .into(imageView)
                } else {
                    imageView.setBackgroundColor(Color.BLUE)
                }
            }
        }
    }
    
  2. object ImageViewAdapter{
        @BindingAdapter("image")
        @JvmStatic
        fun setImage(imageView: ImageView, imageUrl: String?) {
            if (imageUrl.isNotNullOrEmpty()) {
                Glide.with(imageView)
                    .load(imageUrl)
                    .into(imageView)
            } else {
                imageView.setBackgroundColor(Color.BLUE)
            }
        }
    }
    

简单看一下反编译之后的代码,kotlin生成了对应的静态方法,调用了产生的伴生对象

   public static final void setImage(@NotNull ImageView imageView, @Nullable String imageUrl) {
      Companion.setImage(imageView, imageUrl);
   }

那有没有不需要增加注解就直接是静态方法的办法呢?当然有,在kotlin里面顶级函数,就对应java的静态函数,所以可以直接这么写:

直接使用顶级函数就可以了,可以看一下,编译后的java代码:

public final class CustomBindingAdapterKt {
   @BindingAdapter({"image"})
   public static final void setImage(@NotNull ImageView imageView, @Nullable String imageUrl) {
      Intrinsics.checkNotNullParameter(imageView, "imageView");
      if (ExtensionUtilsKt.isNotNullOrEmpty((CharSequence)imageUrl)) {
         Intrinsics.checkNotNullexpressionValue(Glide.with((View)imageView).load(imageUrl).into(imageView), "Glide.with(imageView)n  …         .into(imageView)");
      } else {
         imageView.setBackgroundColor(-16776961);
      }

   }
}

对应java的类名为文件名+kt,对应的方法为static final修饰的方法。
推荐呢还是直接使用顶级函数方便一点。如果使用伴生对象+JvmStatic注解的话,会产生额外的对象。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号