Object is not part of the schema for this Realm

ε祈祈猫儿з 提交于 2019-12-04 00:18:26

问题


As soon as I try to get my object from Realm database, the app crashed and I get this error:

java.lang.RuntimeException: Unable to start activity 
      ComponentInfo{com.repdev.realtimedelijn/com.repdev.realtimedelijn.activity.MainActivity}: 
    java.lang.IllegalArgumentException: Haltes is not part of the schema for this Realm

This is my Activity were it happens

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());
    setContentView(R.layout.activity_main);
    Context context = this;
    View view = this.getWindow().getDecorView();

    realm = Realm.getInstance(getRealmConfiguration());
    RealmResults<Haltes> haltes = realm
            .where(Haltes.class)
            .findAll();
    HaltesRecyclerViewAdapter haltesRecyclerViewAdapter =
            new HaltesRecyclerViewAdapter(this, haltes, true, true);
    RealmRecyclerView realmRecyclerView =
            (RealmRecyclerView) findViewById(R.id.realm_recycler_view);
    realmRecyclerView.setAdapter(haltesRecyclerViewAdapter);
}

and here is the model

Someone an idea how to fix it? public class Haltes implements RealmModel {

@PrimaryKey
private long id;

private String halteNaam;
private String halteNummer;

public long getId() {

    return id;
}

public void setId(long id) {

    this.id = id;
}

public String getHalteNaam() {

    return halteNaam;
}

public void setHalteNaam(String halteNaam) {

    this.halteNaam = halteNaam;
}

public  String getHalteNummer() {

    return halteNummer;
}

public void setHalteNummer(String halteNummer) {

    this.halteNummer = halteNummer;
}

}


回答1:


My problem was solved by declaring apply plugin: 'realm-android' after all other plugins.

App level Gradle

apply plugin: 'android-apt'
apply plugin: 'realm-android'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion



回答2:


had the same problem while using it along side with retrolambda and android-apt. changing the order of plugins in app level build.gradle file worked for me :

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

Github issue : https://github.com/realm/realm-java/issues/3783#issuecomment-260578984




回答3:


In my case I was need to paste kotlin-kapt to app.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt' <<<<<<<<<<the 1st row<<<<<<<<<
apply plugin: 'realm-android' <<<<<the second row<<<<<

I spent 6 hours to solve this problem. And now it works. And how was written above - realm-android should be added to the end of all plugins!




回答4:


Try this: Android Studio -> Build -> Rebuild & Clean Project




回答5:


Are you using the @RealmClass annotation? If you are using annotations, make sure you have annotation processing enabled in your Android studio settings.




回答6:


I believe this has to do with adding a new Realm Model class after some models were already added. try un installing the application and run again or migrate your schema.

Does your Haltes class extends RealmObject?

Make it like this:

public class Haltes extends RealmObject

or

@RealmClass
public class Haltes implements RealmModel



回答7:


You haven't added Realm to your build.gradle file: https://bitbucket.org/repdev/realtimedelijnandroid/src/77c531768dc1250b4d5b5c6c7fd4e6100764177d/build.gradle?at=master&fileviewer=file-view-default

See how here: https://realm.io/docs/java/latest/#installation

Your top level build.gradle file should have this

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "io.realm:realm-gradle-plugin:1.0.1"
  }
}

Your app level build.gradle file should have this at the top:

apply plugin: 'realm-android'



回答8:


I got this exception when using a library project with my app project and realm plugin is only applied to the library project. When I added realm plugin `apply plugin: 'realm-android' to the app project ,the exception gone.

Make sure realm plugin added to every gradle project that uses realm.




回答9:


For those of you who use a mixture of Kotlin in your codebase, then this problem will be solved by applying kotlin-kapt before realm-android.

That is:

apply plugin: 'kotlin-kapt' apply plugin: 'realm-android'

Source: https://github.com/realm/realm-java/issues/5697



来源:https://stackoverflow.com/questions/37887303/object-is-not-part-of-the-schema-for-this-realm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!