Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object is not part of the schema for this Realm

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;
}

}

like image 484
Riyan Fransen Avatar asked Jun 17 '16 17:06

Riyan Fransen


4 Answers

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
like image 160
harsh_v Avatar answered Nov 17 '22 07:11

harsh_v


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

like image 36
Mad Avatar answered Nov 17 '22 07:11

Mad


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!

like image 9
Eugene Voronoy Avatar answered Nov 17 '22 07:11

Eugene Voronoy


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

like image 3
Bob Avatar answered Nov 17 '22 06:11

Bob