Android Kotlin:ParcelableArrayList 适用于 Fragment 参数,但在 intent extra 中使用时崩溃

Android Kotlin : ParcelableArrayList works for Fragment argument but crashed when used in intent extra

我正在尝试在用 Kotlin 编写的 Android 应用程序中使用 ParcelableArrayList 传递一些数据。

数据class很简单:

package com.example.testapp

import android.os.Parcel
import android.os.Parcelable

class Data(val name: String, val size: Long): Parcelable {

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(name)
        dest.writeLong(size)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object {
        val CREATOR = object: Parcelable.Creator<Data> {
            override fun createFromParcel(source: Parcel): Data {
                val name = source.readString()!!
                val size = source.readLong()!!
                return Data(name, size)
            }

            override fun newArray(size: Int) = arrayOfNulls<Data>(size)
        }
    }
}

当我尝试通过片段转换传递它时,它工作正常。但是当我把它放在额外的意图中时它崩溃了,就像这样:

val dataArray = arrayListOf<Data>(Data("AAA", 111L), Data("BBB", 222L))
val intent = Intent(this, SecondActivity::class.java)
intent.putParcelableArrayListExtra("DataList", dataArray)
startActivity(intent)

在第二个 activity 中,我按照以下步骤提取数据:

var dataArray = intent?.getParcelableArrayListExtra<Data>("DataList")!!
dataArray.forEach{
    Log.d("XXX", "Get data name=${it.name}, size=${it.size}")
}

然后我遇到了臭名昭著的崩溃错误:

Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.testapp.Data
        at android.os.Parcel.readParcelableCreator(Parcel.java:3063)
        at android.os.Parcel.readParcelable(Parcel.java:2981)
        at android.os.Parcel.readValue(Parcel.java:2883)
        at android.os.Parcel.readListInternal(Parcel.java:3324)
        at android.os.Parcel.readArrayList(Parcel.java:2467)
        at android.os.Parcel.readValue(Parcel.java:2904)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3261)
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
        at android.os.BaseBundle.unparcel(BaseBundle.java:236)
        at android.os.Bundle.getParcelableArrayList(Bundle.java:1005)
        at android.content.Intent.getParcelableArrayListExtra(Intent.java:8605)
        at com.example.testapp.SecondActivity.onCreate(SecondActivity.kt:12)
        at android.app.Activity.performCreate(Activity.java:7981)
        at android.app.Activity.performCreate(Activity.java:7970)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7762) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047) 

谁能帮忙指出我做错了什么?我整晚都被困在这里!

非常感谢。

尝试用以下代码替换您的数据class

class Data(val name: String, val size: Long): Parcelable {
constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readLong()) {
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeString(name)
    parcel.writeLong(size)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Data> {
    override fun createFromParcel(parcel: Parcel): Data {
        return Data(parcel)
    }

    override fun newArray(size: Int): Array<Data?> {
        return arrayOfNulls(size)
    }
}
}