我有一个很奇怪的问题。这段代码几天前就可以使用了,但是现在它使我的应用程序崩溃了。我正在尝试遍历节点子节点。
private void getInfo()
{
FirebaseDatabase.getInstance().getReference()
.child("ForumResponses").child(forumID).addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot snap : dataSnapshot.getChildren())
{
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
如果我删除下面的代码行,则应用程序不会崩溃。为什么会这样?
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
下面是错误日志
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(com.google.firebase:firebase-database@@16.1.0:351)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database@@16.1.0:272)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:197)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@16.1.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@16.1.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:580)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:550)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.1.0:420)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
at Activities.ViewForumResponseActivity$1.onDataChange(ViewForumResponseActivity.java:95)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
下面是论坛响应类
public class ForumResponses
{
private String id;
private String response;
private String author;
private String authorID;
private String usersWhoLiked;
private int liked;
public ForumResponses()
{
/*
Default no arg constructor
*/
}
public ForumResponses(String id, String response, String author, String authorID, String usersWhoLiked, int liked)
{
this.id = id;
this.response = response;
this.author = author;
this.authorID = authorID;
this.usersWhoLiked = usersWhoLiked;
this.liked = liked;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getResponse()
{
return response;
}
public void setResponse(String response)
{
this.response = response;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getAuthorID()
{
return authorID;
}
public void setAuthorID(String authorID)
{
this.authorID = authorID;
}
public String getUsersWhoLiked()
{
return usersWhoLiked;
}
public void setUsersWhoLiked(String usersWhoLiked)
{
this.usersWhoLiked = usersWhoLiked;
}
public int getLiked()
{
return liked;
}
public void setLiked(int liked)
{
this.liked = liked;
}
}
下面是数据库的图像
答案 0 :(得分:1)
您遇到以下错误:
com.google.firebase.database.DatabaseException:无法将类型java.util.HashMap的值转换为int
因为您的liked
类中的ForumResponses
字段的类型为int
,并且在数据库中,因为该字段是实际的对象。要解决此问题,请将数据库中的字段转换为int
或将ForumResponses
类中的字段类型更改为Map<String, Object>
。