【问题标题】:IllegalArgumentException: XXX is not part of the schema for this RealmIllegalArgumentException: XXX 不是该领域架构的一部分
【发布时间】:2016-12-18 22:54:06
【问题描述】:

每次使用领域启动我的第一个应用程序时,我都会遇到 IllegalArgumentException。我的应用程序从未工作过。我读了很多书试图解决我的问题,我做了很多改变。

Android-Studio 配置:

  • 启用注释

  • 即时运行已禁用

Build.gradle 配置:

  • 领域版本 2.2.1

  • sdk 版本 25

  • 我有应用插件:'com.android.application''realm-android''android-apt'和'android-command'

  • 我有 dexOptions 增量错误

在我的应用程序中

Realm.init(this);

我写迁移代码

public class Migration implements RealmMigration {

    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();
        if(newVersion == 0){
            oldVersion++;
        }
        if(!schema.contains("ItemCatalog")){
            schema.create("ItemCatalog")
                    .addField("id",Integer.class)
                    .addField("tab",Integer.class)
                    .addField("image", byte[].class)
                    .addField("title",String.class)
                    .addField("description",String.class)
                    .addPrimaryKey("id");
        }
    }
}

在我的第一个活动中

RealmConfiguration config = new RealmConfiguration.Builder().migration(new Migration())
                .schemaVersion(0)
                .name("appVenture.realm").build();
        Realm.setDefaultConfiguration(config);

在我的 D.T.O.object 里面

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.RealmClass;

@RealmClass
public class ItemCatalog extends RealmObject{

    @PrimaryKey
    private Integer id = null;
    private Integer tab = null;
    private byte[] image = null;
    private String title = null;
    private String description = null;

//.. auto-generated getters and setters here
}

我最大的例外是

E/AndroidRuntime: 致命异常: main 进程:com.example.francisco.helloworld,PID:1646 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.francisco.helloworld/com.example.francisco.helloworld.MainActivity}: java.lang.IllegalArgumentException:ItemCatalog 不属于 此领域的架构 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370) 在 android.app.ActivityThread.access$800(ActivityThread.java:155) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5426) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 在 dalvik.system.NativeStart.main(本机方法) 原因:java.lang.IllegalArgumentException:ItemCatalog 不是此领域架构的一部分 在 io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172) 在 io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90) 在 io.realm.RealmSchema.getTable(RealmSchema.java:296) 在 io.realm.Realm.createObjectInternal(Realm.java:817) 在 io.realm.Realm.createObject(Realm.java:801) 在 com.example.francisco.helloworld.data.RealmController$1.execute(RealmController.java:72) 在 io.realm.Realm.executeTransaction(Realm.java:1253) 在 com.example.francisco.helloworld.data.RealmController.saveCatalog(RealmController.java:69) 在 com.example.francisco.helloworld.data.RealmController.saveCatalog(RealmController.java:64) 在 com.example.francisco.helloworld.MainActivity.onCreate(MainActivity.java:67) 在 android.app.Activity.performCreate(Activity.java:5296) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370) 在 android.app.ActivityThread.access$800(ActivityThread.java:155) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5426) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 在 dalvik.system.NativeStart.main(Native Method)

我花了 3 天时间试图解决这个问题。

提前谢谢你。

最好的问候。

【问题讨论】:

  • 您是否按照 hello world 教程进行操作?我怀疑您的应用程序中缺少配置

标签: java android realm


【解决方案1】:

而不是

apply plugin: 'com.android.application' 
apply plugin: 'realm-android' 
apply plugin: 'android-apt'
apply plugin: 'android-command'

你需要有

apply plugin: 'com.android.application' 
apply plugin: 'android-apt'
apply plugin: 'realm-android' 
apply plugin: 'android-command'

【讨论】:

  • (如果人们使用 jack { enabled true } 而不是 Retrolambda,他们似乎也会收到此错误)
【解决方案2】:

我今天遇到了同样的问题。我尝试了多种解决方案,但它们对我不起作用。

在我自己的情况下,我已经有一些课程(如RealmChild1 and RealmChild2),它们是extending RealmObject。当我添加一个名为extending RealmObject 的新类 (RealmChild3) 时发生异常。我能够通过卸载我的应用程序并重新安装并再次启动该应用程序来解决这个问题。我怀疑 Realm 已经创建了一个包含 RealmChild1 and RealmChild2 的 Schema,它无法动态添加 RealmChild3。这就是为什么它抱怨RealmChild3 is not part of the Schema for this Realm

帮助这有助于某人。

【讨论】:

  • 你好@berrytchacks 现在我们使用 sqlLite 的那个项目。 (这是我们最好的解决方案)。我通过重新安装解决了其他 Realm 项目,但在该项目中不起作用。 (为您的评论 +1)
【解决方案3】:

我怀疑这是一个配置错误,无论是连接和授权设置,还是您配置的领域对象。

https://realm.io/docs/realm-object-server/

这也是相关的: https://github.com/realm/realm-java/issues/3139

【讨论】:

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