Firebase 评论查看错误 - 反序列化时需要一个列表,但有一个类 java.util.HashMap
Posted
技术标签:
【中文标题】Firebase 评论查看错误 - 反序列化时需要一个列表,但有一个类 java.util.HashMap【英文标题】:Firebase Comments viewing error - Expected a List while deserializing, but got a class java.util.HashMap 【发布时间】:2021-08-24 06:07:11 【问题描述】:当我尝试在列表中显示 cmets 时出现此错误 - com.google.firebase.database.DatabaseException:反序列化时需要一个列表,但得到一个类 java.util.HashMap 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(CustomClassMapper.java:251) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:177) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
我的评论显示活动 -
myRef.child("Photo")
.child(mphoto.getPhoto_id())
.child("comments")
.addChildEventListener(new ChildEventListener()
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName)
Log.d(TAG, "onChildAdded: child added.");
Query query = myRef
.child("Photo")
.orderByChild("photo_id")
.equalTo(mphoto.getPhoto_id());
query.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dsnapshot)
for ( DataSnapshot singleSnapshot : dsnapshot.getChildren())
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
mComments.clear();
Comments firstComment = new Comments();
firstComment.setUser_id(mphoto.getUser_id());
firstComment.setComment(mphoto.getCaption());
firstComment.setDate_created(mphoto.getDate_Created());
mComments.add(firstComment);
List<Comments> commentsList = new ArrayList<Comments>();
for (DataSnapshot dSnapshot : singleSnapshot
.child("comments").getChildren())
Comments comments = new Comments();
comments.setUser_id(dSnapshot.getValue(Comments.class).getUser_id());
comments.setComment(dSnapshot.getValue(Comments.class).getComment());
comments.setDate_created(dSnapshot.getValue(Comments.class).getDate_created());
mComments.add(comments);
photo.setComments(mComments);
mphoto=photo;
setupWidgets();
@Override
public void onCancelled(@NonNull DatabaseError error)
);
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName)
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot)
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName)
@Override
public void onCancelled(@NonNull DatabaseError error)
);
【问题讨论】:
请编辑您的问题并将您的数据库结构添加为 JSON 文件。您可以通过单击 Firebase Console 的溢出菜单 (⠇) 中的导出 JSON 来简单地获取它。 【参考方案1】:错误提示
反序列化时需要一个列表,但得到了一个类 java.util.HashMap
这可能会出错
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
您可以更改它并使其变得简单,如下所示
String caption = singleSnapshot.child("caption").getValue(String.class);
photo.setCaption(objectMap.get("caption").toString());//So you wont able to do this
photo.setCaption(caption);//And you can use like this
而不是使用这个
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
用这个替换它
String caption=singleSnapshot.child("caption").getValue(String.class);
String tags= singleSnapshot.child("tags").getValue(String.class);
String photo_id=singleSnapshot.child("photo_id").getValue(String.class);
String user_id= singleSnapshot.child("user_id").getValue(String.class);
String date_Created = singleSnapshot.child("date_Created").getValue(String.class);
String image_Path= singleSnapshot.child("image_Path").getValue(String.class);
photo.setCaption(caption);
photo.setTags(tags);
photo.setPhoto_id(photo_id);
photo.setUser_id(user_id);
photo.setDate_Created(date_Created);
photo.setImage_Path(image_Path);
你可以看到没有objectMap
【讨论】:
什么错误?你应该替换你的objectMap 错误不是他们的错误在别的地方。 但是你试过了吗?如果我的回答出现错误,那么您可以评论错误是什么。 错误是一样的 - 反序列化时需要一个列表,但有一个类 java。 util.HashMap以上是关于Firebase 评论查看错误 - 反序列化时需要一个列表,但有一个类 java.util.HashMap的主要内容,如果未能解决你的问题,请参考以下文章
FirebaseDatabaseException:反序列化时需要一个列表,但有一个类 java.util.HashMap