22

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

}

Riyan Fransen
  • 534
  • 1
  • 3
  • 14

9 Answers9

25

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
harsh_v
  • 3,193
  • 3
  • 34
  • 53
12

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

Mad
  • 180
  • 2
  • 9
  • 1
    This worked for me. I moved a project to Android Annotations and Realm stopped working, this solved my issue – severin.julien Jan 29 '17 at 19:41
  • If you are using Kotlin, remove `android-apt` plugin and use `kotlin-kapt` instead. That solved it for me. – levibostian Jan 31 '17 at 17:40
  • Putting `realm-android` after `android-apt` worked for me. – Daan Apr 07 '17 at 09:32
  • Worked for me, thanks so much! In my case I had this code: `apply plugin: 'realm-android' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions'` And I've had to move the Realm line to be the last. – Josue Mar 28 '19 at 11:14
10

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!

Eugene Voronoy
  • 1,384
  • 1
  • 14
  • 18
3

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

Bob
  • 127
  • 1
  • 14
  • No I have never used that annotation, I'm just learning to get on my way with Realm so I followed just some tuts., my post is updated with my code btw. – Riyan Fransen Jun 17 '16 at 17:47
  • What version of realm are you using if you are not using the annotations? Also, this is probably a migration issue. Try to clean and rebuild your project or remove the `.apt_generated` folder manually if that doesn't work. – Bob Jun 17 '16 at 17:53
  • Here is a link to my sourcecode https://bitbucket.org/repdev/realtimedelijnandroid/src – Riyan Fransen Jun 17 '16 at 18:09
3

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

Cuong Nguyen
  • 1,166
  • 1
  • 11
  • 20
2

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
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • 1
    Just tried it, doesn't work. I uninstalled the app, even put Realm.deleteRealm(getRealmConfiguration()); in the code for a fresh start. Doesn't help – Riyan Fransen Jun 17 '16 at 18:04
  • Make sure your class extends RealmObject, you mentioned that it implements is as an interface, it should extend it. – Mina Wissa Jun 17 '16 at 18:09
  • I'm fairly new to Android development with libraries so kinda of a new to all of this. Here is a link to my source code https://bitbucket.org/repdev/realtimedelijnandroid/src – Riyan Fransen Jun 17 '16 at 18:13
2

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'
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
2

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

Fadils
  • 1,508
  • 16
  • 21
1

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.

Nidhin
  • 1,818
  • 22
  • 23