java.lang.RuntimeException: Parcel android.os.Parcel: Unmarshalling unknown type code

前端 未结 2 1223
傲寒
傲寒 2021-01-08 01:07

I seem to be getting a strange error in my app (see GitHub), which occurs when I pass objects to different activities that implement Parcelable.

I have

相关标签:
2条回答
  • 2021-01-08 01:38

    Your problem is in LanguagesFlashCard. Here are your parcel/unparcel methods:

    protected LanguagesFlashCard(Parcel in) {
        mId = in.readInt();
        mEnglish = in.readString();
        mAnswerPrefix = in.readString();
        mAnswer = in.readString();
        mTier = in.readInt();
        mTopic = in.readParcelable(Topic.class.getClassLoader());
    }
    

    As you can see, they don't match. The second item you write to the Parcel is an int, the second item you read from the Parcel is a String.

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mId);
        dest.writeInt(mTier);
        dest.writeString(mEnglish);
        dest.writeString(mAnswerPrefix);
        dest.writeString(mAnswer);
        dest.writeParcelable(mTopic, flags);
    }
    
    0 讨论(0)
  • 2021-01-08 01:48

    Kotlin code for sub data class like ImagesModel also parcelable used

    data class MyPostModel(
        @SerializedName("post_id") val post_id: String? = "",
        @SerializedName("images") val images: ArrayList<ImagesModel>? = null
    ): Parcelable {
        constructor(parcel: Parcel) : this(
            parcel.writeString(post_id)
            parcel.createTypedArrayList(ImagesModel.CREATOR)
        )
    
        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeString(post_id)
            parcel.writeTypedList(images)
        }
    }
    
    0 讨论(0)
提交回复
热议问题