很抱歉我问了个愚蠢的问题.

我用谷歌搜索了这个问题,但答案对我来说不适用.

这是我的密码.

  data class Article(val id: Long, val title: String, val ingress: String, val image: String,
                       val dateTime: String, val tags: List<String>, val content: List<Item>, val created: Long, val changed: Long) {

        @delegate:Transient
        val formatDateString: String by lazy {
            val sdf = SimpleDateFormat("dd.MM.yyyy HH.mm")
            val date: Date = sdf.parse(dateTime)
            var time: String?
            if (date.year == Date().year) {
                time = SimpleDateFormat("dd MM,HH:mm", Locale.US).format(date)
            } else {
                time = SimpleDateFormat("dd MM yyyy,HH:mm", Locale.US).format(date)
            }
            time!!
        }

    }

当我试图得到formatDateString个值时,就会出现上述异常.

The codes access formatDateString as following

  override fun getCustomAdapter(): BaseQuickAdapter<CarListResponseBody.Article, BaseViewHolder> {
        return object : BaseQuickAdapter<CarListResponseBody.Article, BaseViewHolder>( R.layout.app_item_cars_list,dataList) {
            override fun convert(helper: BaseViewHolder, item: CarListResponseBody.Article) {
                helper.setText(R.id.tv_ingress,item.ingress)
                helper.setText(R.id.tv_date_time,item.formatDateString)
                helper.setText(R.id.tv_title,item.title)
                ImageLoadUtil.loadImgToView(context,item.image,helper.getView(R.id.img_car),null)
            }
        }
    }

我认为这个异常是由Gson引起的,但我不知道为什么

推荐答案

我不是专业人士,但我想告诉你我的发现.让我们创建一个简单的类来看看Kotlin中的委托是如何工作的.以防万一,我用的是柯特林1.3.70.下面是我们的简单类:

class Test {
    val test by lazy { "test" }
}

在IDEA或Android Studio中,您可以判断字节码.但至少对我来说,阅读JVM字节码相当困难:)让我们将其转换为类似Java的代码:

...
public final class Test {
   @NotNull
   private final Lazy test$delegate;

   @NotNull
   public final String getTest() {
      Lazy var1 = this.test$delegate;
      Object var3 = null;
      boolean var4 = false;
      return (String)var1.getValue();
   }

   public Test() {
      this.test$delegate = LazyKt.lazy((Function0)null.INSTANCE);
   }
}
...

As you see Kotlin creates a special field for lazy delegate which initializes in the constructor. The problem is Gson uses UnsafeAllocator and doesn't call the constructor. You can read a bit more about it for example here. So when you deserialize your Article object it contains null instead of appropriate lazy delegate. That's why you got a null pointer exception when trying to call this del

Kotlin相关问答推荐

如何在Kotlin中反射多个对象以查找特定类型的属性

只能在元素区域中点击的Jetpack Compose列

Kotlin -从列表中获取特定过滤器的唯一列表值

带有Spring Boot和Kotline的可嵌入实体

如何为集成测试配置Gradle JVM测试套件?

无法访问类kotlin.coroutines.CoroutineContext';.判断模块类路径中是否存在丢失或冲突的依赖项

在调用父构造函数之前重写类属性

多模块Kotlin项目中的FreeFair

Webpack 配置未应用于 kotlin 多平台react 项目

按钮无法在 Android Studio 上打开新活动

在 kotlin 中重载函数时,我在一些非常基本的代码上不断收到类型不匹配

从列表中的每个对象中 Select 属性

带有迭代器函数的 Kotlin 无限序列

将ExpectedException与Kotlin一起使用

是否在Kotlin中重写enum toString()?

Kotlin:如何修改成对的值?

Kotlin协程无法处理异常

spring.config.location 在 Spring Boot 2.0.0 M6 上不起作用

Kotlin替换字符串中的多个单词

Kotlin:在何时使用枚举