gpt4 book ai didi

android - 结合使用 Firebase 和 Realm 时发生崩溃 - : Lio/reactivex/Observable; 的解析失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:21 34 4
gpt4 key购买 nike

我正在尝试从 Firebase 下载对象并将它们存储在 Realm 中。结合这两个优秀的库时,我遇到了崩溃。

我创建了一个非常简单的演示应用程序来说明我的问题。在创建扩展 RealmObject 的对象时,Firebase 似乎感到不安。

我可以通过删除 RealmObject 扩展来完成这项工作,但我必须维护两个相同的对象。随着时间的推移,这变得复杂和令人沮丧,这远非理想。

Firebase:

    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
}

Gradle :

compile 'com.google.firebase:firebase-database:11.8.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'

Realm :

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)

最佳答案

这类类加载器问题可以通过两种方式解决:

1.) 为任何缺失添加一个虚拟类,参见 this answer详情

2.) 添加compile "io.reactivex.rxjava2:rxjava:2.1.8" 到依赖项

关于android - 结合使用 Firebase 和 Realm 时发生崩溃 - : Lio/reactivex/Observable; 的解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185420/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com