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

前端 未结 10 1904
小鲜肉
小鲜肉 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条回答
  •  萌比男神i
    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? = 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 = object : Parcelable.Creator {
            override fun createFromParcel(source: Parcel): Nota = Nota(source)
            override fun newArray(size: Int): Array = 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 {
        override fun createFromParcel(parcel: Parcel): CentroCusto {
            return CentroCusto(parcel)
        }
    
        override fun newArray(size: Int): Array {
            return arrayOfNulls(size)
        }
    }
    
    }
    

提交回复
热议问题