How to fix Unmarshalling unknown type code XXX at offset YYY in Android?

前端 未结 10 1872
小鲜肉
小鲜肉 2020-12-05 07:04

I\'m having app crash on resume because of Unmarshalling exception. I\'ve checked all the Serializables have constructor with no parameters and even checked all the serializ

相关标签:
10条回答
  • 2020-12-05 07:13

    Using Kotlin, the object 'Nota' has a Custom object 'CentroCusto' that contains only primitive properties.

    class Nota(var id: Int? = null, var centroCusto: ArrayList<CentroCusto>? = null) : Parcelable {
    
    constructor(source: Parcel) : this(
            source.readValue(Int::class.java.classLoader) as Int?,
            //Exception in below line -> Unmarshalling unknown type code 
            source.createTypedArrayList(CentroCusto.CREATOR) 
    )
    
    override fun describeContents() = 0
    
    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeValue(id)
        writeTypedList(centroCusto)
    }
    
    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Nota> = object : Parcelable.Creator<Nota> {
            override fun createFromParcel(source: Parcel): Nota = Nota(source)
            override fun newArray(size: Int): Array<Nota?> = arrayOfNulls(size)
        }
    }
    

    The CentroCusto class:

    class CentroCusto(var id: Int? = null, var descricao: String? = null, var aprovacaoDiretoria: Int? = null, var permiteMarcar: Boolean? = null) : Parcelable {
    
    constructor(parcel: Parcel) : this(
            parcel.readValue(Int::class.java.classLoader) as? Int,
            parcel.readString(),
            parcel.readValue(Int::class.java.classLoader) as? Int,
            parcel.readValue(Boolean::class.java.classLoader) as? Boolean) {
    }
    
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeValue(id)
        parcel.writeString(descricao)
        parcel.writeValue(aprovacaoDiretoria)
        parcel.writeValue(permiteMarcar)
    }
    
    override fun describeContents(): Int {
        return 0
    }
    
    companion object CREATOR : Parcelable.Creator<CentroCusto> {
        override fun createFromParcel(parcel: Parcel): CentroCusto {
            return CentroCusto(parcel)
        }
    
        override fun newArray(size: Int): Array<CentroCusto?> {
            return arrayOfNulls(size)
        }
    }
    
    }
    
    0 讨论(0)
  • 2020-12-05 07:15

    It's because of Proguard obfuscation - it processed Parcelable. solution: Proguard causing RuntimeException (Unmarshalling unknown type code) in Parcelable class

    0 讨论(0)
  • 2020-12-05 07:15

    If a type of List is added, it should be:

    @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeList(this.mList);
    

    and unparcel it using the class type like this:

    protected MyClass(Parcel in) {
            super(in);
            this.mList = new ArrayList<>();
            in.readList(this.mList, MyRequiredClass.class.getClassLoader());
    
    0 讨论(0)
  • In my case my class don't added CREATOR filed when extending from another Parcelable parent.

    public class Image implements Parcelable {
            protected Image(Parcel in) {
            }
    
            public static final Creator<Image> CREATOR = new Creator<Image>() {
                @Override
                public Image createFromParcel(Parcel in) {
                    return new Image(in);
                }
    
                @Override
                public Image[] newArray(int size) {
                    return new Image[size];
                }
            };
    
            @Override
            public int describeContents() {
                return 0;
            }
    
            @Override
            public void writeToParcel(Parcel dest, int flags) {
            }
    }
    
    public class ImageChild extends Image {
            protected ImageChild(Parcel in) {
                super(in);
            }
    
    
            @Override
            public int describeContents() {
                return 0;
            }
    
            @Override
            public void writeToParcel(Parcel dest, int flags) {
                super.writeToParcel(dest, flags);
            }
    }
    

    In ImageChild missing CREATOR field

    0 讨论(0)
  • 2020-12-05 07:16

    There's some rules for write and read parcelables.

    1- Be careful about type mismatch. If you write int, don't try to read long etc.

    2- Be careful about order of writes and reads. Objects must be at the same sort order when reading and writing.

    Both of these can cause "Unmarshalling unknown type code".

    0 讨论(0)
  • 2020-12-05 07:16

    In my case it was caused by data size exceeding max limit of that can be put to Parcel which is about 1 MB.

    Solution: optimize your Parcelable code to put as less data as possible (e.g. do not put Serializable objects to Parcel)

    0 讨论(0)
提交回复
热议问题