IllegalArgumentException:XXX不是此域的架构的一部分

时间:2016-12-18 22:54:06

标签: java android realm

每次我用realm开始我的第一个应用程序时,我都有IllegalArgumentException。我的申请从未奏效。我读了很多试图解决我的问题,并做了很多改变。

Android-Studio配置:

  • 已启用注释

  • 立即停止运行

Build.gradle config:

  • 领域版本2.2.1

  • sdk version 25

  • 我已申请插件:'com.android.application''realm-android''android-apt'和'android-command'

  • 我有dexOptions incremental false

在我的应用程序中,我有

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:致命异常:主要                     过程: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)                         at 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)                         at dalvik.system.NativeStart.main(Native Method)                      引起:java.lang.IllegalArgumentException:ItemCatalog不是此Realm架构的一部分                         at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)                         at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)                         at io.realm.RealmSchema.getTable(RealmSchema.java:296)                         at io.realm.Realm.createObjectInternal(Realm.java:817)                         at 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)                         at 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(本地方法)

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

提前谢谢。

最诚挚的问候。

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分:1)

我今天遇到了同样的问题。我尝试了多种解决方案,但他们并没有为我工作。

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

帮助此人。

答案 2 :(得分:0)

我怀疑这是配置错误,无论是使用连接和授权设置,还是使用已配置的域对象。

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

这也有关系: https://github.com/realm/realm-java/issues/3139