Class X is not abstract and does not implement fun writeToParcel() defined in android.os.Parcelable

你离开我真会死。 提交于 2020-01-13 08:18:10

问题


In my Android app, I want to add a Bundle including a Place object described below to my Intent. Since serializable was slow and not recommended, I preferred Parcelable.

Althoug I use Kotlin 1.3.31, I have problems parcelizing some data classes. Example:

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Place(val street: String, val postal: String, val city: String) : Parcelable

and Android Studio complains:

Class 'Place' is not abstract and does not implement abstract member public abstract fun writeToParcel(p0: Parcel!, p1: Int): Unit defined in android.os.Parcelable

According to some tutorials

That’s it! You don’t need to write any parcel methods anymore!

https://android.jlelse.eu/yet-another-awesome-kotlin-feature-parcelize-5439718ba220

and I do not want to use

androidExtensions {
    experimental = true
}

in production stuff.

What alternatives would I have here?


回答1:


UPDATE 19/11/2019

After the stable release of Kotlin 1.3.60 and its corresponding Android Studio plugin the issue is no more. Let's celebrate

UPDATE 27/08/2019

After a bit of more researching and testing with the brand new Kotlin 1.3.50 seems that the issue is going to be finally fully addressed when they release Kotlin 1.3.60 as per this YouTrack issue

EDIT 19/06/2019

With Kotlin 1.3.40 release the @Parcelize annotation is out of experimental and works quite nicely. The only issue is a reported issue that makes the IDE go red, leaving this to a side the code does compile and run perfectly.

I have also tested with this kind of objects and it does also work:

Old answer

I'm facing the exact same issue and while investigating I found this:

So the looks like the @Parcelize annotation will be fully stable starting from Kotlin 1.3.40. Until then you will have to set the experimental flag. (Sadly)


They have wrongly pushed @Parcelize outside experimental features and you still get that compilation error.




回答2:


You can always just implement Parcelable(cmd+enter/alt+enter on class name -> `Add Parcelable Implementation" in order for IDE to create method schemas for you) if you dont' want to/can't use experimental extensions.

I've, however, been using @Parcelize in prod environment(after a lot of testing though) for over a year, with no issues.(Just an opinion though, not saying you should)



来源:https://stackoverflow.com/questions/56018761/class-x-is-not-abstract-and-does-not-implement-fun-writetoparcel-defined-in-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!