realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

java.lang.IllegalArgumentException: User is not part of the schema for this Realm

Open xiaosheng1126 opened this issue 10 years ago • 37 comments

       at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:132)
                                                                  at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:79)
                                                                  at io.realm.Realm.getTable(Realm.java:1270)
                                                                  at io.realm.Realm.createObject(Realm.java:695)
                                                                  at com.putao.wechat.MainActivity$1.onClick(MainActivity.java:43)
                                                                  at android.view.View.performClick(View.java:4630)
                                                                  at android.view.View$PerformClick.run(View.java:19331)
                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:157)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5387)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                                                                  at dalvik.system.NativeStart.main(Native Method)

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

String moduleName = "io.realm.DefaultRealmModule"; tracking Down This class does not exist.

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

It means the annotation processor isn't running for some reason. Are you using Android Studio?

cmelchior avatar Aug 04 '16 09:08 cmelchior

i use the tools android studio.

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

Can you post the gradle log from a clean build?

cmelchior avatar Aug 04 '16 09:08 cmelchior

/**

  • Created by Administrator on 2016/8/4. */ public class User extends RealmObject {

    @PrimaryKey private String userId;

    private String userName;

    public String getUserId() { return userId; }

    public void setUserId(String userId) { this.userId = userId; }

    public String getUserName() { return userName; }

    public void setUserName(String userName) { this.userName = userName; } }

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

RealmConfiguration configuration = new RealmConfiguration.Builder(this).build(); Realm.setDefaultConfiguration(configuration); findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); User user = realm.createObject(User.class); user.setUserId(100 + ""); user.setUserName("jjj"); realm.commitTransaction(); request(CacheType.SERVER_ONLY);

        }
    });

    findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            request(CacheType.CACHE_ONLY);
            User user = new User();
            user.setUserId(100 + "");
            user.setUserName("ddd");
            DBHelper.save(user);
        }
    });

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

apply plugin: 'com.android.application' apply plugin: 'realm-android' android { compileSdkVersion 24 buildToolsVersion "24.0.1"

defaultConfig {
    applicationId "com.putao.wechat"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile project(':library') compile project(':http') }

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' classpath "io.realm:realm-gradle-plugin:1.1.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }

allprojects { repositories { jcenter() } }

task clean(type: Delete) { delete rootProject.buildDir }

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

My english is poor , you do chinese?

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

@beeender can 😄

cmelchior avatar Aug 04 '16 09:08 cmelchior

I can use chinese to communicate with you right

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

Yes

cmelchior avatar Aug 04 '16 09:08 cmelchior

前几天demo演示的时候貌似还可以用,今天就不可以了,一直报上面这个问题。

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

是 kotlin 工程么?还是 java 和 kotlin 混合的工程?

beeender avatar Aug 04 '16 09:08 beeender

我跟踪发现RealmConfiguration public static Object getDefaultModule() { String moduleName = "io.realm.DefaultRealmModule"; Class clazz; //noinspection TryWithIdenticalCatches try { clazz = Class.forName(moduleName); Constructor constructor = clazz.getDeclaredConstructors()[0]; constructor.setAccessible(true); return constructor.newInstance(); } catch (ClassNotFoundException e) { return null; } catch (InvocationTargetException e) { throw new RealmException("Could not create an instance of " + moduleName, e); } catch (InstantiationException e) { throw new RealmException("Could not create an instance of " + moduleName, e); } catch (IllegalAccessException e) { throw new RealmException("Could not create an instance of " + moduleName, e); } } 返回了null,

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

混合吧

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

要不我把源码发给你看看

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

kotlin 和 java 混合的时候注解处理器会又点问题。

你试试把 apply plugin: 'kotlin-android' 放到 apply plugin: 'realm-android' 前面

如果不行的话,把源码发给我。发到 [email protected] 或者其他你方便的方式。

beeender avatar Aug 04 '16 09:08 beeender

// Creates the mediator that defines the current schema private RealmProxyMediator createSchemaMediator(Builder builder) {

    Set<Object> modules = builder.modules;
    Set<Class<? extends RealmModel>> debugSchema = builder.debugSchema;

    // If using debug schema, use special mediator
    if (debugSchema.size() > 0) {
        return new FilterableMediator(DEFAULT_MODULE_MEDIATOR, debugSchema);
    }

    // If only one module, use that mediator directly
    if (modules.size() == 1) {
        return getModuleMediator(modules.iterator().next().getClass().getCanonicalName());
    }

    // Otherwise combine all mediators
    RealmProxyMediator[] mediators = new RealmProxyMediator[modules.size()];
    int i = 0;
    for (Object module : modules) {
        mediators[i] = getModuleMediator(module.getClass().getCanonicalName());
        i++;
    }
    return new CompositeMediator(mediators);
}

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126

public CompositeMediator(RealmProxyMediator... mediators) { final HashMap<Class<? extends RealmModel>, RealmProxyMediator> tempMediators = new HashMap<>(); if (mediators != null) { for (RealmProxyMediator mediator : mediators) { for (Class<? extends RealmModel> realmClass : mediator.getModelClasses()) { tempMediators.put(realmClass, mediator); } } } this.mediators = Collections.unmodifiableMap(tempMediators); }

xiaosheng1126 avatar Aug 04 '16 09:08 xiaosheng1126