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

Caused by: java.lang.ClassCastException:

Open RoyXing opened this issue 9 years ago • 7 comments

02-20 20:54:27.154 1655-1655/com.lecai.dev E/CrashHandler: uncaught exception java.lang.RuntimeException: Unable to destroy activity {com.lecai.dev/com.lecai.activity.VideoPlayActivity}: java.lang.ClassCastException: io.realm.StudyTimeRealmProxy cannot be cast to com.lecai.bean.StudyTime at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5070) at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5093) at android.app.ActivityThread.access$1700(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1853) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7233) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.ClassCastException: io.realm.StudyTimeRealmProxy cannot be cast to com.lecai.bean.StudyTime at java.lang.Class.cast(Class.java:1511) at io.realm.DefaultRealmModuleMediator.newInstance(DefaultRealmModuleMediator.java:145) at io.realm.internal.modules.CompositeMediator.newInstance(CompositeMediator.java:102) at io.realm.BaseRealm.get(BaseRealm.java:511) at io.realm.Realm.createObjectInternal(Realm.java:871) at io.realm.Realm.createObject(Realm.java:847) at com.lecai.utils.StudyUtils.saveKnowledgeHistoryPosition(StudyUtils.java:74) at com.lecai.activity.VideoPlayActivity.subStudy(VideoPlayActivity.java:326) at com.lecai.activity.VideoPlayActivity.onDestroy(VideoPlayActivity.java:321) at android.app.Activity.performDestroy(Activity.java:7102) at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1170) at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5048) at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5093)  at android.app.ActivityThread.access$1700(ActivityThread.java:221)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1853)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7233)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  Realm version(s): ? 2.3.1 Realm sync feature enabled: yes/no yes Android Studio version: ? 2.3Beta4 Which Android version and device: ? android 6.0

RoyXing avatar Feb 20 '17 12:02 RoyXing

I use realm in library and app, but in app it's useless

RoyXing avatar Feb 20 '17 12:02 RoyXing

Hi @RoyXing Are you defining StudyTime model class in both app and library?

zaki50 avatar Feb 20 '17 13:02 zaki50

@zaki50 No, i don't , but if i define it in both app and library.It works. Why?

RoyXing avatar Feb 20 '17 13:02 RoyXing

Did you specify local module AND library module? https://github.com/realm/realm-java/blob/master/examples/moduleExample/app/src/main/java/io/realm/examples/appmodules/ModulesExampleActivity.java#L68

Zhuinden avatar Feb 20 '17 13:02 Zhuinden

@Zhuinden Yes,I do it like this example.

apply plugin: 'com.android.library'
apply plugin: 'maven'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'realm-android'

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.yxt.sdk:library_photoviewer:0.2.0'
//    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
//    compile project(':library_autolayout')
//    compile 'com.yxt.sdk.library_http:library_networkstate:0.2.0'
    compile project(':library_log_base')
    compile project(':library_pdf_viewer')
//    compile project(':library_committee_utils')
//    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:palette-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile('com.lecai.library:library_imkit:1.10.0') {
        exclude group: 'com.lecai.library', module: 'library_pulltorefreshview'
    }
    compile 'com.yxt.sdk:library_share:0.2.0'
    compile 'com.yxt.sdk:library_meeting:2.2.3'
    compile 'com.yxt.sdk:library_course:1.1.8'
//    compile 'com.google.code.gson:gson:2.2.1'
    compile('com.yxt.sdk:library_xuanke:1.4.3') {
        exclude module: 'library_http_base'
        exclude module: 'library_webview_base'
        exclude module: 'library_comment'
        exclude module: 'library_photoviewer'
    }
    compile 'com.yxt.sdk:library_permission:1.0.1'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.lecai"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 20803
        versionName "2.08.03"
        multiDexEnabled true

        ndk {
            abiFilters 'armeabi', 'armeabi-v7a' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }
   
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile project(':library_community')
    compile project(':library_zxing')
    compile project(':library_webview_base')
//    compile project(':library_comment')
//    annotationProcessor 'com.jakewharton:butterknife-compiler:latest.release'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

RoyXing avatar Feb 20 '17 13:02 RoyXing

@RoyXing Can you share a project which reproduce this issue?

zaki50 avatar Feb 20 '17 13:02 zaki50

@zaki50 Because the permission in android 6.0+,I think the Realm need storage permission, so i wrote the Realm.init(this); RealmConfiguration configuration = new RealmConfiguration .Builder() .name("") .modules(Realm.getDefaultModule(), new FrameModule()) .deleteRealmIfMigrationNeeded() .build(); Realm.setDefaultConfiguration(configuration);not in application. When i remove the code in application ,it work.It's embarrassing!

RoyXing avatar Feb 21 '17 02:02 RoyXing