问题描述
我已经在Kotlin data
类中将自动生成用于Parcelable
的实现.相同的类在Room
中用作Entity
,因此对于每个构造函数参数我都有默认值,以为Room
提供一个空的构造函数.当我尝试将此项目作为Parcelable
传递时,应用程序崩溃并显示UnsupportedOperationException
和此消息
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 to provide an empty constructor to Room
. When I try to pass this item as Parcelable
the app crashes with UnsupportedOperationException
and this message
现在的解决方法是,我只是用Gson
序列化对象并将其作为String
传递,但我想知道是否有适当的方法来处理Kotlin和Parcelable
中的默认值.
The work around for now I have , I just serialize the object with Gson
and pass it as String
but I would like to know if there is proper way to handle default values in Kotlin and Parcelable
.
这是课程
@Entity
data class ProductData(
//primary key here
@ ColumnInfo val alcoholRestriction: Boolean = false,
@Ignore val averageWeight: Double = 0.0,
@Ignore val hotFoodRestriction: Boolean = false,
@Ignore val maxOrderValue: Double = 0.0,
@Ignore val minOrderValue: Double = 0.0,
@Ignore val orderIncrement: Double = 0.0,
@ColumnInfo var productId: String = "",
@Ignore val stdUomCd: String = "",
@Ignore val uomQty: String = ""
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readByte() != 0.toByte(),
parcel.readDouble(),
parcel.readByte() != 0.toByte(),
parcel.readDouble(),
parcel.readDouble(),
parcel.readDouble(),
parcel.readString(),
parcel.readString(),
parcel.readString()
)
override fun writeToParcel(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)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ProductData> {
override fun createFromParcel(parcel: Parcel): ProductData {
return ProductData(parcel)
}
override fun newArray(size: Int): Array<ProductData?> {
return arrayOfNulls(size)
}
}
}
试图通过是捆绑销售,崩溃发生在此行.
Trying to pass is in a bundle and the crash happens on this line.
findNavController().navigate(
Uri.parse("android-app://androidx.navigation/productDetails/$item"))
在导航图中将其设置为自定义的可打包变量
it is set in the nav graph as custom Parcelable variable
推荐答案
将默认值移动到构造函数中,无论如何它们几乎都是无用的.布尔值默认为false,并加倍为零.只有琴弦需要一些东西.
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,
"",
"",
"")
override fun writeToParcel(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)
}
这篇关于Android Parcelable不支持默认值App Crash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!