【问题标题】:Using android-extensions`s Parcelize annotation on objects that extends Parceable sealed classes在扩展 Parceable 密封类的对象上使用 android-extensions 的 Parcelize 注释
【发布时间】:2018-08-16 19:36:55
【问题描述】:

我正在使用 kotlin android 扩展来自动生成我的 Parcelables,但鉴于以下代码,我得到了 'Parcelable' should be a class

代码:

sealed class Action : Parcelable

@Parcelize
object Run : Action()

@Parcelize
data class Ask(
    val question: String
) : Action()

我的理解是,不可能像我那样在object 中使用@Parcelize(一旦它在Ask 类上工作)。

我想在扩展sealed class 的对象中使用Parcelable 注释,所以我不知道该怎么做,也不要编写以下样板。

object Run : Action() {

    override fun writeToParcel(p0: Parcel?, p1: Int) {}
    override fun describeContents() = 0

    @JvmField
    val CREATOR = object : Parcelable.Creator<Run> {
        override fun createFromParcel(parcel: Parcel) = Run
        override fun newArray(size: Int) = arrayOfNulls<Run?>(size)
    }

}

【问题讨论】:

  • 密封类不能加@Parcelize吗?
  • @EpicPandaForce 不,我们不能在密封或抽象类上使用它。

标签: kotlin annotations parcelable kotlin-android-extensions


【解决方案1】:

你需要确保你的 Kotlin 是最新的。

您可以关注example here

【讨论】:

  • 非常感谢,我用的是1.2.51的版本,但是1.2.60的版本解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
相关资源
最近更新 更多