【问题标题】:kotlin - realm and parcelkotlin - 领域和包裹
【发布时间】:2016-08-12 04:59:33
【问题描述】:

我使用 android studio 开发 android 项目。 我想在 kotlin 中使用 onSaveInstanceState() 保存领域对象。 我的代码是

@Parcel( implementations = arrayOf(UserRealmProxy::class),
         value = Parcel.Serialization.BEAN,
         analyze = arrayOf(User::class))
open class User : RealmObject() {
    open var name: String? = null

    @ParcelPropertyConverter(ListParcelConverter::class)
    open var Items: RealmList<Item>? = null
}

但是编译时出现一些错误:

'Unresolved reference: UserRealmProxy'
'An annotation parameter must be a compile-time constant'

当然,UserRealmProxy 已经存在,因为项目已经编译。 @ParcelPropertyConverter(ListParcelConverter::class) 也不起作用。 它会在运行时导致异常:

'java.io.NotSerializableException: io.realm.RealmList'

但是这段代码在java中编译得很好。

我需要你的帮助。

【问题讨论】:

  • 需要包裹而不是重新查询对象吗?
  • 首先我收到了相同的消息Unresolved reference: UserRealmProxy,但在构建项目后它消失了。
  • 你能用@field:ParcelPropertyConverter(ListParcelConverter::class)代替@ParcelPropertyConverter(ListParcelConverter::class)吗?
  • 我也有同样的问题。你解决了吗?
  • 同样的问题..你找到解决方案了吗?

标签: android realm kotlin


【解决方案1】:

您可能会为 setter 进行注释。请试试这个。

@Parcel( implementations = arrayOf(UserRealmProxy::class),
     value = Parcel.Serialization.BEAN,
     analyze = arrayOf(User::class))
open class User : RealmObject() {
    open var name: String? = null
    open var Items: RealmList<Item>? = null
    @ParcelPropertyConverter(ListParcelConverter::class) set
}

【讨论】:

    【解决方案2】:

    就我而言,当我遇到此错误时:“未解析的引用:UserRealmProxy”。如果我有这个包:com.path.model.Album,我必须使用:

    import io.realm.com_path_model_AlbumRealmProxy
    
    ...
    @Parcel(implementations = arrayOf(com_path_model_AlbumRealmProxy::class)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2014-05-03
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多