简体   繁体   English

使用新的ref.updateChildren()的Android Firebase 2.4 IllegalStateException

[英]Android Firebase 2.4 IllegalStateException using new ref.updateChildren()

When using Firebase 2.4 ref.updateChildren() with HashMap, other than HashMap<String, Object> (eg HashMap<String, User> ) getting the IllegalStateException. 当使用Firebase 2.4 ref.updateChildren()和HashMap时,除了HashMap<String, Object> (例如HashMap<String, User> )之外,获取IllegalStateException。

> 09-29 18:03:21.680: E/AndroidRuntime(6863): FATAL EXCEPTION: main
> 09-29 18:03:21.680: E/AndroidRuntime(6863): Process:
> com.xxx.xxx.xxx, PID: 6863 09-29
> 18:03:21.680: E/AndroidRuntime(6863): java.lang.IllegalStateException:
> Could not execute method of the activity 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> android.view.View$1.onClick(View.java:4035) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> android.view.View.performClick(View.java:4881) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> android.view.View$PerformClick.run(View.java:19592) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> android.os.Handler.handleCallback(Handler.java:733) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> android.os.Handler.dispatchMessage(Handler.java:95) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> android.os.Looper.loop(Looper.java:146) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> android.app.ActivityThread.main(ActivityThread.java:5756) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> java.lang.reflect.Method.invokeNative(Native Method) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> java.lang.reflect.Method.invoke(Method.java:515) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
> 09-29 18:03:21.680: E/AndroidRuntime(6863):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> dalvik.system.NativeStart.main(Native Method) 09-29 18:03:21.680:
> E/AndroidRuntime(6863): Caused by:
> java.lang.reflect.InvocationTargetException 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> java.lang.reflect.Method.invokeNative(Native Method) 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> java.lang.reflect.Method.invoke(Method.java:515) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   at
> android.view.View$1.onClick(View.java:4030) 09-29 18:03:21.680:
> E/AndroidRuntime(6863):   ... 11 more 09-29 18:03:21.680:
> E/AndroidRuntime(6863): Caused by:
> com.firebase.client.FirebaseException: Failed to parse node with class
> class com.xxx.xxx.xxx.User 09-29
> 18:03:21.680: E/AndroidRuntime(6863):     at
> com.firebase.client.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:84)
> 09-29 18:03:21.680: E/AndroidRuntime(6863):   at
> com.firebase.client.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:12)
> 09-29 18:03:21.680: E/AndroidRuntime(6863):   at
> com.firebase.client.utilities.Validation.parseAndValidateUpdate(Validation.java:127)
> 09-29 18:03:21.680: E/AndroidRuntime(6863):   at
> com.firebase.client.Firebase.updateChildren(Firebase.java:438)

EDIT : 编辑

Is there a way to pass custom HashMap like HashMap<String, User> to ref.updateChildren() ? 有没有办法将自定义HashMap像HashMap<String, User>传递给ref.updateChildren()

On your last question: 关于你的上一个问题:

Is there a way to pass custom HashMap like HashMap to ref.updateChildren() ? 有没有办法将自定义HashMap像HashMap传递给ref.updateChildren()?

The Firebase SDK for Android/Java doesn't support directly passing POJOs (like your User class) into updateChildren() . 适用于Android / Java的Firebase SDK不支持直接将POJO(如您的User类)传递到updateChildren()

But what you can do is convert the POJO into a Map with: 但你可以做的是将POJO转换为Map

Map<String, Object> userMap = new ObjectMapper().convertValue(user, Map.class);

Then you can put the map of values into the values that you're passing into updateChildren() : 然后,您可以将值映射放入您传递给updateChildren()的值中:

updates.put("users/"+uid, userMap);
updates.put("lists/"+uid+"/mine, "value");

ref.updateChildren(updates);

As in the code snippet from the Official Firebase Blog to avoid this issue one has to re-write custom structure like this: 官方Firebase博客的代码段一样,要避免此问题,必须重新编写自定义结构,如下所示:

Map<String, String> newPost = new HashMap<String, String>();
newPost.put("title", "New Post");
newPost.put("content", "Here is my new post!");

And then put it instead of custom data model: 然后把它代替自定义数据模型:

Map<String, Object> updatedUserData = new HashMap<String, Object>();
updatedUserData.put("users/posts/" + newPostKey, true);
updatedUserData.put("posts/" + newPostKey, newPost);

Using custom data model instead of newPost causes IllegalStateException. 使用自定义数据模型而不是newPost会导致IllegalStateException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ref.updateChildren()中的FirebaseException:路径X是更新中X / Y的祖先 - FirebaseException in ref.updateChildren(): Path X is an ancestor of X/Y in an update Android Studio Firebase更新儿童 - Android Studio Firebase updateChildren 在Android上处理Firebase DatabaseReference updateChildren OnCompletionListener的正确方法 - Right way to handle Firebase DatabaseReference updateChildren OnCompletionListener on Android 如何知道 Firebase 实时数据库上的 updateChildren() 是否成功? - how to know if updateChildren() on Firebase Realtime database was successful or not? 多查询和引用 Firebase 数据库 Android - Multiple Query and Ref Firebase Database Android Android Studio - 使用改造时的 IllegalStateException - Android Studio - IllegalStateException While Using Retrofit 如何在Android Studio中使用Firebase在新活动中显示通知数据? - how to show the notification data in new activity using firebase in android studio? 在Android中使用ViewPager时出现java.lang.IllegalStateException - java.lang.IllegalStateException while using ViewPager in Android 将 Web 应用程序升级到 Spring Boot 2.4 后的 IllegalStateException - IllegalStateException after upgrading web app to Spring Boot 2.4 解决Android / OpenGL IllegalStateException - Resolving Android / OpenGL IllegalStateException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM