简体   繁体   English

如何在Kotlin @Parcelize中使用null

[英]How to use null with kotlin @Parcelize

Here is very simple code: 这是非常简单的代码:

@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())
    }
}

I have nullable property Test.inner . 我有可为空的属性Test.inner If it is not null, the code works fine but when it is null then I have the exception: 如果它不为null,则代码可以正常工作,但是当它为null时,我会遇到异常:

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

How to resolve this? 如何解决呢? Maybe I do something wrong. 也许我做错了。

该错误已被固定在科特林1.1.60。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM