Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

时间:2022-10-22 16:19:45

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/

为了使用Parcelable,我遵循了此版本的Kotlin 1.1.4:https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/

Add this line in project

在项目中添加此行

androidExtensions {
    experimental = true
}

Then define a class :

然后定义一个类:

@Parcelize
class User(val firstName: String, val lastName: String) : Parcelable

The writeToParcel() and createFromParcel() methods are created automatically

writeToParcel()和createFromParcel()方法是自动创建的

override fun writeToParcel(parcel: Parcel, flags: Int) {
...
}

but still have an error in 'override' keyword with message

但仍然在带有消息的'override'关键字中出错

OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED: Overriding 'writeToParcel' is not allowed. Use 'Parceler' companion object instead

OVERRIDING_WRITE_TO_PARCEL_IS_NOT_ALLOWED:不允许覆盖'writeToParcel'。请改用“Parceler”伴侣对象

Can you show me the right way?

你能告诉我正确的方法吗?

Edit : Does only properties defined in default constructor will be add to Parcel, and other is not? I see this warning in this class.

编辑:只有默认构造函数中定义的属性是否会添加到Parcel,而其他属性不是?我在这节课中看到了这个警告。

PROPERTY_WONT_BE_SERIALIZED: Property would not be serialized into a 'Parcel'. Add '@Transient' annotation to remove the warning

PROPERTY_WONT_BE_SERIALIZED:属性不会被序列化为“包裹”。添加“@Transient”注释以删除警告

2 个解决方案

#1


3  

Make sure you are using the kotlin 1.1.4 version

确保您使用的是kotlin 1.1.4版本

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:

无需重写writeToParcel / createFromParcel方法。除非你做任何特定的事情。工作室给你错误,但你可以忽略这个错误; lint检查尚未更新以了解@Parcelize。相应的YouTrack问题在这里:

https://youtrack.jetbrains.com/issue/KT-19300

https://youtrack.jetbrains.com/issue/KT-19300

To use create class

要使用create class

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

Then to pass it like

然后传递它就像

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

To get it back

要取回它

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

#2


4  

You can safely just ignore the warning as it's only lint check.
for now to get rid of it just use @SuppressLint("ParcelCreator")

你可以安全地忽略警告,因为它只是lint检查。现在要摆脱它只需使用@SuppressLint(“ParcelCreator”)

ex:

例如:

@SuppressLint("ParcelCreator")
@Parcelize
class User(val firstName: String, val lastName: String) : Parcelable

#1


3  

Make sure you are using the kotlin 1.1.4 version

确保您使用的是kotlin 1.1.4版本

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:

无需重写writeToParcel / createFromParcel方法。除非你做任何特定的事情。工作室给你错误,但你可以忽略这个错误; lint检查尚未更新以了解@Parcelize。相应的YouTrack问题在这里:

https://youtrack.jetbrains.com/issue/KT-19300

https://youtrack.jetbrains.com/issue/KT-19300

To use create class

要使用create class

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

Then to pass it like

然后传递它就像

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

To get it back

要取回它

Android Kotlin创建类实现Parcelable在writeToParcel方法的'override'中给出错误

#2


4  

You can safely just ignore the warning as it's only lint check.
for now to get rid of it just use @SuppressLint("ParcelCreator")

你可以安全地忽略警告,因为它只是lint检查。现在要摆脱它只需使用@SuppressLint(“ParcelCreator”)

ex:

例如:

@SuppressLint("ParcelCreator")
@Parcelize
class User(val firstName: String, val lastName: String) : Parcelable