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(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshotchildSnapshot: dataSnapshot.getChildren()) {
String val=childSnapshot.getValue(String.class);
arrayList.add(val);
}
}
@OverridepublicvoidonCancelled(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(newChildEventListener() {
@OverridepublicvoidonChildAdded(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 ""