【问题标题】:How to use null with kotlin @Parcelize如何在 kotlin @Parcelize 中使用 null
【发布时间】:2018-04-03 03:56:56
【问题描述】:

这是非常简单的代码:

@Parcelize
data class Inner(val a: Int): Parcelable

@Parcelize
data class Test(val a: Int, val inner: Inner?): Parcelable

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun testParcel() {
        val test = Test(0, null)

        val parcel = Parcel.obtain()
        parcel.writeParcelable(test, test.describeContents())
    }
}

我有可以为空的属性Test.inner。如果它不为空,则代码可以正常工作,但是当它为空时,我会遇到异常:

java.lang.NullPointerException: Attempt to invoke interface method 'void android.os.Parcelable.writeToParcel(android.os.Parcel, int)' on a null object reference

如何解决这个问题?也许我做错了什么。

【问题讨论】:

  • 这些文件是在test文件夹还是androidTest文件夹?
  • @SangeetSuresh 它在androidTest中
  • @SilviaH 你是对的。谢谢。

标签: android kotlin parcelable kotlin-android-extensions


【解决方案1】:

该错误已在 Kotlin 1.1.60 中为 fixed

【讨论】:

  • Kotlin 1.3.11 发生在我身上:/
  • 还有 1.3.21 !
  • 打了1.3.50的bug
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
相关资源
最近更新 更多