Skip to content Skip to sidebar Skip to footer

Android Parcelable Don't Support Default Values App Crash

I have used autogeneration for Parcelable implementation in a Kotlin data class. The same class is used as an Entity in Room so I have default values for every constructor params t

Solution 1:

Move your defaults to the constructor, they are mostly useless anyway. Boolean defaults to false and doubles to zero. Only the strings need something.

Also, why make the strings nullable if you assign a default value anyway.

@Entity
data class ProductData(
//primary key here
@ ColumnInfo val alcoholRestriction: Boolean,
@Ignore val averageWeight: Double,
@Ignore val hotFoodRestriction: Boolean,
@Ignore val maxOrderValue: Double,
@Ignore val minOrderValue: Double,
@Ignore val orderIncrement: Double,
@ColumnInfo var productId: String,
@Ignore val stdUomCd: String?,
@Ignore val uomQty: String?
) : Parcelable {
constructor(parcel: Parcel) : this(
    parcel.readByte() != 0.toByte() ?: false,
    parcel.readDouble() ?: 0.0,
    parcel.readByte() != 0.toByte() ?: false,
    parcel.readDouble() ?: 0.0,
    parcel.readDouble() ?: 0.0,
    parcel.readDouble() ?: 0.0,
    parcel.readString() ?: "", 
    parcel.readString() ?: "",
    parcel.readString() ?: ""
)

constructor() : this(
   false, 
   0.0,
   false,
   0.0,
   0.0,
   0.0,
   "",
   "",
   "")

overridefunwriteToParcel(parcel: Parcel, flags: Int) {
    parcel.writeByte(if (alcoholRestriction) 1 else 0)
    parcel.writeDouble(averageWeight)
    parcel.writeByte(if (hotFoodRestriction) 1 else 0)
    parcel.writeDouble(maxOrderValue)
    parcel.writeDouble(minOrderValue)
    parcel.writeDouble(orderIncrement)
    parcel.writeString(productId)
    parcel.writeString(stdUomCd)
    parcel.writeString(uomQty)
}

Post a Comment for "Android Parcelable Don't Support Default Values App Crash"