对象不是Kotlin中此Realm的架构的一部分

时间:2018-05-11 13:49:57

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

我有一个这样的公开课

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)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

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

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