Home » Android » android – E/Parcel: Class not found when unmarshalling class with @Parcelize-Exceptionshub

android – E/Parcel: Class not found when unmarshalling class with @Parcelize-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

so i got Retrofit API + Gson as converter and i have parcelable class to pass to various activities…

@Parcelize
data class GenericList<T>(
    @SerializedName("data") var data: @RawValue List<T>
) : Parcelable

Here is a generic list class which is parcelable with following:

abstract class AbsPlot : Parcelable {

    abstract var rating: Map<String, Rating>?
    abstract var plots: GenericList<Plot>?
    ....
    abstract var languages: GenericList<Language>?

}

@Parcelize
data class Plot(
        @SerializedName("rating") override var rating: Map<String, Rating>?,
        @SerializedName("plots") override var plots: GenericList<Plot>?,
        ....
        @SerializedName("languages") override var languages: GenericList<Language>?,
) : AbsPlot()

Once i want to un-parcel those class, it crashes with
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling

What can be a solution?

How to&Answers:

The problem could be the @RawValue annotation.

From its javadoc:

Write the corresponding property value with [android.os.Parcel.writeValue].
Serialization may fail at runtime, depending on actual property value type.

It may be having some trouble serializing the generic type T. That’s all I can guess without more details on which class couldn’t be found.

I would suggest that you avoid using that annotation and use the compiler generics options like so:

@Parcelize
data class GenericList<T : Parcelable>(
    @SerializedName("…") val data: List<T>
) : Parcelable

I may also suggest, if it could be possible, making AbsPlot an interface and mark as Parcelable each of the implementations of the interface.

Another thing that makes me doubt is to check whether the class Ratings is Parcelable.