简体   繁体   中英

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

}

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

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

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!

试试这个:Android Studio-> Build-> Rebuild&Clean Project

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

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

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'

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM