对象不是此领域模式的一部分

2022-09-02 00:19:23

当我尝试从 Realm 数据库获取对象时,应用程序崩溃了,我收到以下错误:

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

这是我的活动,如果它发生

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

这是模型

有人一个想法如何解决它?公共类 Haltes 实现了 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

我的问题通过在所有其他插件之后声明来解决。apply plugin: 'realm-android'

应用级级别

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

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

答案 2

在将其与逆行者和android-apt一起使用时遇到了同样的问题。更改应用程序级文件中插件的顺序对我有用:build.gradle

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

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


推荐