0
votes

Consider:

mDatabase = FirebaseDatabase.getInstance().getReference("archive");
mDatabase.orderByChild("_age").equalTo("20").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        value = dataSnapshot.getValue(String.class);
        tSResult.setText(value);
    }

    @Override
    public void onCancelled(DatabaseError error) {

        Log.w(TAG, "Failed to read value.", error.toException());
    }
});

Firebase Database.

tSResult is a TextView I'm using just to test the returned data.

I'm trying to run this code, but my application either crashes or does not return any result.

Log:

04-26 19:51:24.146 4372-4372/com.example.joe.orside E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.joe.orside, PID: 4372
com.google.firebase.database.DatabaseException: Failed to convert
value of type java.util.HashMap to String
at com.google.android.gms.internal.zzbqi.zzaD(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at
com.example.joe.orside.MainActivity$8$1.onDataChange(MainActivity.java:755)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Any suggestions?

3
post crash log.Kishore Jethava
@KishoreJethava I tried. It also crashed.Joe Bou Ghazali

3 Answers

3
votes

You have to iterate your DataSnapshot

Query  query = mDatabase.orderByChild("_age").equalTo("20");
            query .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.getChildrenCount() > 0){
                        System.out.println("count : "+ dataSnapshot.getChildrenCount());
                        for(DataSnapshot child : dataSnapshot.getChildren()){
                           value = child.getValue(String.class);
                            tSResult.setText(value);
                        }
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
0
votes

It's crashing because of this:

value = dataSnapshot.getValue(String.class);

Change it to:

Hashmap<String,Object> value = (HashMap<String, Object>)dataSnapshot.getValue();
0
votes
Query query = mDatabase.orderByChild("_age").equalTo("20");
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getChildrenCount() > 0){
            String a = "";
            int count = (int)dataSnapshot.getChildrenCount();
            for(DataSnapshot child : dataSnapshot.getChildren()){
                Emergency value = child.getValue(Emergency.class);
                a = a + value.get_name() + "\n";
            }
            tSResult.setText(count + "\n" + a);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});