Android Kotlin创建类实现Parcelable在writeToParcel方法的“覆盖"中给出错误 [英] Android Kotlin create class implement Parcelable give an error in 'override' of writeToParcel method
问题描述
要使用Parcelable,我遵循了Kotlin 1.1.4的此发行版:
To use Parcelable, I had followed this release of Kotlin 1.1.4 : https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/
将此行添加到项目中
androidExtensions {
experimental = true
}
然后定义一个类:
@Parcelize
class User(val firstName: String, val lastName: String) : Parcelable
writeToParcel()和createFromParcel()方法是自动创建的
The writeToParcel() and createFromParcel() methods are created automatically
override fun writeToParcel(parcel: Parcel, flags: Int) {
...
}
但是在带有消息的"override"关键字中仍然存在错误
but still have an error in 'override' keyword with message
OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED:覆盖'writeToParcel' 不允许.改用包裹"辅助对象
OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED: Overriding 'writeToParcel' is not allowed. Use 'Parceler' companion object instead
您能告诉我正确的方法吗?
Can you show me the right way?
是否仅将默认构造函数中定义的属性添加到Parcel中,其他不添加?我在这堂课上看到了这个警告.
Edit : Does only properties defined in default constructor will be add to Parcel, and other is not? I see this warning in this class.
PROPERTY_WONT_BE_SERIALIZED:属性不会序列化为 '包裹'.添加"@Transient"注释以删除警告
PROPERTY_WONT_BE_SERIALIZED: Property would not be serialized into a 'Parcel'. Add '@Transient' annotation to remove the warning
推荐答案
确保您使用的是Kotlin 1.1.4版本
Make sure you are using the kotlin 1.1.4 version
无需重写writeToParcel/createFromParcel方法. 除非您做任何特定的事情.工作室会给您错误,但您可以忽略此错误;皮棉支票尚未更新以了解@Parcelize.相应的YouTrack问题在这里:
No need to override the writeToParcel/createFromParcel methods. Unless you doing any specific things.The studio gives you error but you can ignore this error; the lint checks haven’t yet been updated to understand @Parcelize. The corresponding YouTrack issue is here:
https://youtrack.jetbrains.com/issue/KT-19300
使用创建类
然后像这样传递它
找回
这篇关于Android Kotlin创建类实现Parcelable在writeToParcel方法的“覆盖"中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!