我正在尝试从Firebase下载对象并将它们存储在Realm中。结合这两个优秀的库,我收到了崩溃。
我创建了一个非常简单的演示应用程序来说明我的问题。在创建扩展RealmObject的对象时,Firebase似乎感到不安。
我可以通过删除RealmObject扩展来完成这项工作,但是我必须维护两个相同的对象。随着时间的推移,这变得复杂和令人沮丧,这远非理想。
火力地堡:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("players");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: " + dataSnapshot.getChildrenCount());
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//***CRASH here*****
Player player = snapshot.getValue(Player.class);
Log.d(TAG, "onDataChange: " + player.getFirstname());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled: ", databaseError.toException());
}
});
播放器:
public class Player extends RealmObject{
private int age;
private String firstname;
public Player() {
}
//getters and setters
}
摇篮:
compile 'com.google.firebase:firebase-database:11.8.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
服务器:
classpath 'io.realm:realm-gradle-plugin:4.3.1'
错误:
FATAL EXCEPTION: main
Process: com.test.testrealm, PID: 7616
java.lang.NoClassDefFoundError: Failed resolution of: Lio/reactivex/Observable;
at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method)
at java.lang.reflect.Method.getReturnType(Method.java:141)
at java.lang.Class.getDeclaredMethods(Class.java:1880)
at com.google.android.gms.internal.zzelx.<init>(Unknown Source:310)
at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
at com.google.android.gms.internal.zzelw.zzb(Unknown Source:182)
at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.test.testrealm.MainActivity$2.onDataChange(MainActivity.java:41)
at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.reactivex.Observable" on path: DexPathList[[zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/base.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/lib/arm64, /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/base.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
01-10 10:24:06.881 7616-7616/com.test.testrealm E/AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 19 more
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.<init>(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.<init>(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:35)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:693)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:727)
at android.app.LoadedApk.getResources(LoadedApk.java:954)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2270)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
答案 0 :(得分:7)
这种类加载器问题可以通过两种方式解决:
1。)要么为缺少的内容添加一个虚拟类,请参阅this answer了解详细信息
2。)将compile "io.reactivex.rxjava2:rxjava:2.1.8"
添加到依赖项
答案 1 :(得分:0)
领域docs说:
扩展RealmObject基类的另一种方法是实现 RealmModel接口并添加@RealmClass批注。
然后,可通过静态方法获得RealmObject上所有可用的方法 方法。
如果添加@RealmClass
批注并实现接口RealmModel
,则可以使用Firebase像fireDoc.getObject(Model::class.jaava)
来反序列化对象
答案 2 :(得分:-1)
实现“ io.reactivex.rxjava2:rxjava:2.1.8
添加依赖项