有没有一种方法可以对Kotlin数据类和构造函数使用Parceler进行序列化,而不对每个字段使用@ParcelProperty
注释?
如果我try 像这样使用库:
@Parcel
data class Valve @ParcelConstructor constructor(val size: Int)
I get Error:Parceler: No corresponding property found for constructor parameter arg0
. But if I add @ParcelProperty("size")
it works just fine.
Why is that?
Update:
There are other another way to use this library.
I could just remove @ParcelConstructor
annotation, but then I will get error
Error:Parceler: No @ParcelConstructor annotated constructor and no default empty bean constructor found.
I think (haven't tested it) I also could make all constructor parameters optional and add @JvmOverloads
but that has a side effect that I have to check all properties of the class if they are null or not.
Update 2:
This is what worked for me:
@Parcel
data class Valve(val size: Int? = null)
简而言之,生成的Java类必须有默认的空构造函数.实现这一点的一种方法是如上所述——所有变量都应该有默认值.