【问题标题】:Object is not part of the schema for this Realm对象不是此领域架构的一部分
【发布时间】:2016-10-19 15:07:33
【问题描述】:

当我尝试从 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;
}

}

【问题讨论】:

    标签: java android realm


    【解决方案1】:

    你在使用@RealmClass注解吗? 如果您使用注解,请确保您在 Android Studio 设置中启用了注解处理。

    【讨论】:

    • 不,我从未使用过该注释,我只是在学习如何使用 Realm,所以我只是跟着一些 tuts。,顺便说一句,我的帖子更新了我的代码。
    • 如果您不使用注释,您使用的是什么版本的领域?此外,这可能是一个迁移问题。尝试清理并重建您的项目或手动删除 .apt_generated 文件夹,如果这不起作用。
    • 这里是我的源代码链接bitbucket.org/repdev/realtimedelijnandroid/src
    【解决方案2】:

    我相信这与在添加了一些模型之后添加新的领域模型类有关。尝试卸载应用程序并再次运行或迁移您的架构。

    您的 Haltes 类是否扩展了 RealmObject?

    把它变成这样:

    public class Haltes extends RealmObject
    

    @RealmClass
    public class Haltes implements RealmModel
    

    【讨论】:

    • 刚试过,不行。我卸载了应用程序,甚至把 Realm.deleteRealm(getRealmConfiguration());在代码中重新开始。没用
    • 确保你的类扩展了RealmObject,你提到它实现的是一个接口,它应该扩展它。
    • 我对使用库的 Android 开发还很陌生,所以对所有这些来说都是新事物。这是我的源代码的链接bitbucket.org/repdev/realtimedelijnandroid/src
    【解决方案3】:

    您尚未将 Realm 添加到您的 build.gradle 文件中:https://bitbucket.org/repdev/realtimedelijnandroid/src/77c531768dc1250b4d5b5c6c7fd4e6100764177d/build.gradle?at=master&fileviewer=file-view-default

    在此处查看方法:https://realm.io/docs/java/latest/#installation

    你的顶级build.gradle文件应该有这个

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

    您的应用级别 build.gradle 文件应该在顶部有这个:

    apply plugin: 'realm-android'
    

    【讨论】:

      【解决方案4】:

      在我的应用程序项目中使用库项目时出现此异常,并且领域插件仅应用于库项目。 当我将领域插件 `apply plugin: 'realm-android' 添加到应用项目时,异常消失了。

      确保将领域插件添加到每个使用领域的 gradle 项目中。

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          在与 retrolambdaandroid-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

          【讨论】:

          • 这对我有用。我将一个项目移动到 Android Annotations 并且 Realm 停止工作,这解决了我的问题
          • 如果您使用 Kotlin,请删除 android-apt 插件并改用 kotlin-kapt。这为我解决了。
          • realm-android放在android-apt之后对我有用。
          • 为我工作,非常感谢!在我的情况下,我有这个代码:apply plugin: 'realm-android' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' 我不得不将 Realm 线移到最后。
          【解决方案7】:

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

          应用级 Gradle

          apply plugin: 'android-apt'
          apply plugin: 'realm-android'
          
          android {
              compileSdkVersion rootProject.ext.compileSdkVersion
              buildToolsVersion rootProject.ext.buildToolsVersion
          

          【讨论】:

          • 非常感谢,我在这上面花了很长时间。像魅力一样工作。
          【解决方案8】:

          在我的情况下,我需要将 kotlin-kapt 粘贴到 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<<<<<
          

          我花了 6 个小时来解决这个问题。现在它可以工作了。 以及上面是怎么写的——realm-android应该加到所有插件的末尾!

          【讨论】:

            【解决方案9】:

            对于那些在代码库中混合使用 Kotlin 的人,可以通过在 realm-android 之前应用 kotlin-kapt 来解决此问题。

            即:

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

            来源: https://github.com/realm/realm-java/issues/5697

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多