Android - startActivityForResult onActivityResult 返回 ArrayList

Android - startActivityForResult onActivityResult get ArrayList back

这是我发送 ArrayList 的方式:

val extras = Bundle()

val i = Intent(context, GalleryShow::class.java)
i.putExtra("list", ArrayList(memes))
(context as Activity).startActivityForResult(i, 777)

并在 Activity B:

中接收
intent.getParcelableArrayListExtra<Memes>("list") as ArrayList<Memes>

然后在修改它之后我设置了应该返回的意图 Activity A:

private lateinit var finishIntent: Intent

finishIntent = Intent()

finishIntent.putExtra("list", ArrayList(modifiedMemes))
setResult(RESULT_OK, finishIntent)

并尝试在 A:

中接收它
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, intent)

    if(requestCode == 777 && resultCode == RESULT_OK && data != null) {

        if(data.hasExtra("list")) {
            val updatedMemes = intent.getParcelableArrayListExtra<Memes>("list") as ArrayList<Memes>
        }
    }
}

所以我将其设置为与从 a 到 b 发送和接收的方式完全相同,但我收到错误消息:

kotlin.TypeCastException: null cannot be cast to non-null

val updatedMemes = intent.getParcelableArrayListExtra<Memes>("list") as ArrayList<Memes>

这里有什么问题? google 真的在乎 android 是一堆功能失调的狗屎吗?

您的阅读意图有误 - 您使用的是 intent.getParcelableArrayListExtra<Memes>("list") as ArrayList<Memes>,指的是 Activity 和 intent 属性不是 您从 onActivityResult():

收到的 data Intent
val updatedMemes = data.getParcelableArrayListExtra<Memes>("list") as ArrayList<Memes>