“对象”不是这个域的模式的一部分

当我调用realm.where(MessageEventModel::class.java).findAll()

它抛出excepiton:这是错误的

 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? = 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" } } 

我尝试了一切,我可以在堆栈上find: 干净的生成,重建项目,启用注释处理器,重新安装apk,使缓存无效/重新启动

在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' 

就这样,现在一切正常