Dominic Dominic - 4 months ago 27
Android Question

Android Firebase: Can´t add ValueEventListener

database = FirebaseDatabase.getInstance();
myRef = database.getReference("lifts");


Why can't I add the ValueEventListener? I tried the sample from the Firebase Guide.

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
})


But this works and I have no idea why? Please help me.

myRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot item: dataSnapshot.getChildren()) {

Answer

The error is, that you have to use a com.google.firebase.database.ValueEventListener. But I think you have an import for com.firebase.client.ValueEventListener in your imports on top of the file.

Because of this, the compiler tries to use com.firebase.client.ValueEventListener as default, which obviously fails.

To fix this, look through your imports and remove the entry for com.firebase.client.ValueEventListener. Then you can add com.google.firebase.database.ValueEventListener

This approach doesn't work if you have to use both of them. Then there's no other solution than using the full names (e.g. com.google.firebase.database.ValueEventListener instead of just ValueEventListener) for both or at least one.

Comments