这是错误日志 我在从Firebase实时数据库检索数据时遇到问题。 java.lang.ClassCastException:无法将java.lang.String强制转换为java.util.Map
E/AndroidRuntime: FATAL EXCEPTION: main
Process: root.example.com.chatrack, PID: 31556
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at root.example.com.chatrack.ChatActivity.AmbilDataGroup(ChatActivity.java:156)
at root.example.com.chatrack.ChatActivity.access$200(ChatActivity.java:48)
at root.example.com.chatrack.ChatActivity$2.onDataChange(ChatActivity.java:140)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.6:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.6:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.6:55)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
我不知道出了什么问题 这是我用来检索数据的代码
DatabaseReference add = mFirebaseDatabase.getReference().child("CHATRACK").child("USER").child(Child);
Log.d(TAG, "getFriendsInfo() returned: " + add);
add.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange() returned: " + dataSnapshot);
Log.d(TAG, "onDataChange() returned: " + Child.size());
AmbilDataGroup((Map<String, Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void AmbilDataGroup(Map<String, Object> dataSnapshot) {
final ArrayList<String> Nama = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataSnapshot.entrySet())
{
Map nama= (Map) entry.getValue();
Nama.add((String) nama.get("Nama"));
}
}
为什么会这样。在其他活动中,相同的代码也没问题。 我不明白。请帮助
修改
这里是我的数据库结构 enter image description here
答案 0 :(得分:0)
String value = "{first_name = naresh,last_name = kumar,gender = male}";
value = value.substring(1, value.length()-1); //remove curly brackets
String[] keyValuePairs = value.split(","); //split the string to creat key-value pairs
Map<String,String> map = new HashMap<>();
for(String pair : keyValuePairs) //iterate over the pairs
{
String[] entry = pair.split("="); //split the pairs to get key and value
map.put(entry[0].trim(), entry[1].trim()); //add them to the hashmap and trim whitespaces
}
将此代码添加到
DatabaseReference add = mFirebaseDatabase.getReference().child("CHATRACK").child("USER").child(Child);
Log.d(TAG, "getFriendsInfo() returned: " + add);
add.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange() returned: " + dataSnapshot);
Log.d(TAG, "onDataChange() returned: " + Child.size());
String value = dataSnapshot.getValue();
value = value.substring(1, value.length()-1); //remove curly brackets
String[] keyValuePairs = value.split(","); //split the string to creat key-value pairs
Map<String,String> map = new HashMap<>();
for(String pair : keyValuePairs) //iterate over the pairs
{
String[] entry = pair.split("="); //split the pairs to get key and value
map.put(entry[0].trim(), entry[1].trim()); //add them to the hashmap and trim whitespaces
}
AmbilDataGroup(map);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void AmbilDataGroup(Map<String, Object> dataSnapshot) {
final ArrayList<String> NamaGroup = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataSnapshot.entrySet()) {
Map namaGroup = (Map) entry.getValue();
NamaGroup.add((String) namaGroup.get("GroupName"));
}
final ArrayList<String> GroupMember = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataSnapshot.entrySet()) {
Map groupMember = (Map) entry.getValue();
GroupMember.add((String) groupMember.get("GroupMember"));
}
final ArrayList<String> GroupId = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataSnapshot.entrySet()) {
Map groupId = (Map) entry.getValue();
GroupId.add((String) groupId.get("GroupId"));
}
String[] member = GroupMember.toString().split(",");
String memberId;
if (GroupId != null) {
int i = 0;
while (GroupId.size() > i) {
memberId = GroupMember.get(i).replace("[", "").replace("]", "");
Log.d(TAG, "AmbilDataGroup() returned: " + GroupMember.get(i).split(","));
if (memberId.contains(UserId)) {
}
Log.d(TAG, "AmbilDataGroup() returned: int i" + i);
i++;
}
}
}