【发布时间】:2017-03-22 09:25:01
【问题描述】:
当我拨打realm.where(MessageEventModel::class.java).findAll()时
它抛出异常: 这是错误
java.lang.IllegalArgumentException: MessageEventModel is not part of the schema for this Realm
at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)
at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)
这是我的应用程序类
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.name("my_db")
.build()
Realm.setDefaultConfiguration(realmConfiguration)
}
}
这是我的领域模型
class MessageEventModel : RealmObject{
constructor()
var message = ""
constructor(message: String) : this(){
this.message = message
}
}
这是我试图检索模型的地方
class AwesomeChatFragment : Fragment() {
private val realm: Realm by lazy { Realm.getDefaultInstance() }
private var notifications: RealmResults<MessageEventModel>? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.activity_awesome_chat, container, false)
notifications = realm.where(MessageEventModel::class.java).findAll()
return view
}
}
gradle 配置:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.google.gms:google-services:3.0.0'
classpath "io.realm:realm-gradle-plugin:3.0.0"
}
}
我尝试了所有可以在堆栈上找到的东西: 清理构建、重建项目、启用注释处理器、重新安装 apk、使缓存无效/重新启动
【问题讨论】:
-
我前段时间遇到过这个问题(不同的领域版本)。看看这是否有帮助:github.com/realm/realm-java/issues/2491
-
申请
kotlin-android和kotlin-android-extensinos之前realm-android -
你是天才