You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Activity A向Activity B传递Parcelable对象时getExtra返回null

Fixing Null Parcelable When Passing from Activity A to B

Hey there, let's troubleshoot why your Parcelable A object is showing up as null in Activity B. Based on the code snippet you shared, here are the key fixes and checks to make:

1. Ensure Class B Fully Implements Parcelable

Since your A class holds a MutableList<B>, B must be a properly implemented Parcelable too. If B's Parcelable logic is broken, createTypedArrayList(B.CREATOR) will return null, which can break the entire deserialization of A.

Here's an example of a correct Parcelable implementation for B:

class B(val someField: String) : Parcelable {
    // Parcel constructor
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "" // Add default to avoid null issues
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(someField)
    }

    override fun describeContents(): Int = 0

    companion object CREATOR : Parcelable.Creator<B> {
        override fun createFromParcel(parcel: Parcel): B = B(parcel)
        override fun newArray(size: Int): Array<B?> = arrayOfNulls(size)
    }
}

2. Verify Intent Passing & Retrieval Code

Double-check that you're using the same key and correct type handling when sending and receiving the Parcelable:

In Activity A (sending):

val aInstance = A(1, "Test Name", mutableListOf(B("Sample B")))
val intent = Intent(this, ActivityB::class.java)
intent.putExtra("EXTRA_A_OBJECT", aInstance) // Use a consistent key
startActivity(intent)

In Activity B (receiving):

// For API 33+, use this type-safe method
val aObject = intent.getParcelableExtra("EXTRA_A_OBJECT", A::class.java)

// For older APIs, use this (ensure no type mismatch)
val aObject = intent.getParcelableExtra<A>("EXTRA_A_OBJECT")

Make sure the key matches exactly between send and receive—typos here are a common culprit!

3. Complete A Class's Parcelable Implementation

Your snippet cuts off at companion ...—you must implement the CREATOR for A to enable Parcelable deserialization. Here's the full companion object you need:

class A(var id: Int, var name: String, var bList: MutableList<B>) : Parcelable {
    override fun writeToParcel(p0: Parcel, p1: Int) {
        p0.writeInt(id)
        p0.writeString(name)
        p0.writeTypedList(bList)
    }

    override fun describeContents(): Int = 0

    constructor(parcel: Parcel) : this(
        parcel.readInt(),
        parcel.readString() ?: "", // Add default for non-null name
        parcel.createTypedArrayList(B.CREATOR) ?: mutableListOf() // Default empty list if null
    )

    // Missing CREATOR from your snippet—add this!
    companion object CREATOR : Parcelable.Creator<A> {
        override fun createFromParcel(parcel: Parcel): A = A(parcel)
        override fun newArray(size: Int): Array<A?> = arrayOfNulls(size)
    }
}

Notice we also added default values for name and bList to prevent nulls if the Parcel returns empty values.

4. Test with Simplified Objects

If you're still getting null, test with a minimal A instance (e.g., empty bList or a single simple B object). If this works, the issue is likely in one of the B instances in your original list—check for invalid data or broken Parcelable logic in those objects.


内容的提问来源于stack exchange,提问作者Frank.Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:23:26