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

Java Android Firebase 2.4 IllegalStateException使用新的ref.updateChildren(),java,android,firebase,Java,Android,Firebase,将Firebase 2.4ref.updateChildren()与HashMap一起使用时,除了HashMap(例如HashMap)之外,获取非法状态异常 > 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 >

将Firebase 2.4
ref.updateChildren()
与HashMap一起使用时,除了
HashMap
(例如
HashMap
)之外,获取非法状态异常

> 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)
编辑


是否有一种方法可以将自定义HashMap(如
HashMap
传递到
ref.updateChildren()

中的代码片段中,要避免此问题,必须像这样重新编写自定义结构:

Map<String, String> newPost = new HashMap<String, String>();
newPost.put("title", "New Post");
newPost.put("content", "Here is my new post!");
Map newPost=newhashmap();
newPost.put(“标题”、“新职位”);
放置(“内容”,“这是我的新帖子!”);
然后将其替换为自定义数据模型:

Map<String, Object> updatedUserData = new HashMap<String, Object>();
updatedUserData.put("users/posts/" + newPostKey, true);
updatedUserData.put("posts/" + newPostKey, newPost);
Map updatedUserData=newhashmap();
updateUserData.put(“users/posts/”+newPostKey,true);
updateUserData.put(“posts/”+newPostKey,newPost);
使用自定义数据模型而不是
newPost
会导致非法状态异常。

关于上一个问题:

> 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)
是否有方法将自定义HashMap(如HashMap)传递给ref.updateChildren()

Firebase SDK for Android/Java不支持直接将POJO(如您的
用户
类)传递到
updateChildren()

但您可以通过以下方式将POJO转换为
映射

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

非常感谢,弗兰克!这种方法比将用户逐个字段转换为地图要简洁得多。