当我拨打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)
这是我的Application类
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,使缓存无效/重新启动
答案 0 :(得分:10)
gradle文件中的问题。由于@ EpicPandaForce的评论,只是一个应用插件的排序规则的问题,问题已经解决了,我正在写答案,帮助其他人,如果他们错过@EpicPandaForce的评论答案
我改变了
的顺序apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
到
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
这就是全部,现在一切正常