Firebase Android Error "Failed To Convert Value Of Type "
This code is crashing on start; E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.manya.eventspage, PID: 2974 com.google.firebas
Solution 1:
Your code fails to take care of the fact that you have a list of events under /events/events
. To handle this correctly you have two approaches:
- Use a
ValueEventListener
and loop over the child nodes - Use a
ChildEventListener
The first is closest to your current code and just adds an extra loop:
myRef=database.getReference().child("events/events"); // note the change in path
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String val=childSnapshot.getValue(String.class);
arrayList.add(val);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
The second approach tells Firebase to handle the list in its SDK and surfaces each child:
myRef=database.getReference().child("events/events"); // note the change in path
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {
String val=dataSnapshot.getValue(String.class);
arrayList.add(val);
}
...
});
Post a Comment for "Firebase Android Error "Failed To Convert Value Of Type ""