【问题标题】:Object is not part of the schema for this Realm in Kotlin对象不是 Kotlin 中此领域架构的一部分
【发布时间】:2018-05-11 13:49:57
【问题描述】:

我有一节这样的公开课

open class NewsResponse(

@field:SerializedName("news")
val news: List<NewsItem?>? = null
):RealmObject()

NewsItem 类是这样的

open class NewsItem(

@field:SerializedName("created")
val created: String? = null,

@field:SerializedName("link")
val link: String? = null,

@field:SerializedName("description")
val description: String? = null,

@field:SerializedName("title")
val title: String? = null
):RealmObject()

我也加了

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android-extensions'

app gradle 中的这些插件

我在项目级别的 gradle 中有 classpath "io.realm:realm-gradle-plugin:5.1.0"。 所以当我运行应用程序时,我得到一个错误提示

Caused by: io.realm.exceptions.RealmException: NewsItem is not part of the schema for this Realm
    at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:180)

如何解决这个问题?

【问题讨论】:

    标签: android kotlin realm kotlin-android-extensions android-ktx


    【解决方案1】:

    使用这个顺序:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'
    apply plugin: 'realm-android'
    apply plugin: 'io.fabric'
    

    【讨论】:

    • 我在使用最新的 kotlin 版本和 android studio 时遇到了同样的问题。即使实现了上述解决方案,应用程序仍然会严重崩溃
    • 我听说如果您在某处仍有annotationProcessor 范围而不是kapt,也会发生这种情况
    • 我只有实现和androidTestImplementation。没有注释处理器。也许 Realm 插件是 self 使用此语句。如果是这样,我们无法控制它作为它的 gradle 应用依赖项。
    • 如果kotlin-kaptrealm-android之前应用,那么Realm将在2.2.0左右应用kapt范围
    • 我想通了。 apply plugin: 'kotlin-kapt 丢失了,是的 apply plugin: 'realm-android' 是最后一个应用的。非常感谢!
    【解决方案2】:

    对我来说,我必须将我的订单更改为:

    apply plugin: 'com.android.application'
    apply plugin: 'realm-android'
    apply plugin: 'kotlin-android'
    

    并且还在我的模型类中添加一个空的构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多