32

I am using firebase to retrieve data from database n use

Map<String, String> map = dataSnapshot.getValue(Map.class);

to get values, but it shows error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.rana.sahaj.myyu, PID: 13179
                                                                 com.google.firebase.database.DatabaseException: Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead
                                                                     at com.google.android.gms.internal.zzaix.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zzaix.zza(Unknown Source)
                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                     at com.rana.sahaj.myyu.profile.Profile$2.onDataChange(Profile.java:158)
                                                                     at com.google.android.gms.internal.zzafp.zza(Unknown Source)
                                                                     at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
                                                                     at com.google.android.gms.internal.zzags$1.run(Unknown Source)
                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                     at dalvik.system.NativeStart.main(Native Method)

here's the code

 DatabaseReference profileRef=mFirebaseRef.child(EEmail);
    profileRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

    -->     Map<String, String> map = (Map<String,String>)dataSnapshot.getValue(Map.class);

            name = (TextView)findViewById(R.id.UserInActivity);
            EmailView= (TextView)findViewById(R.id.emailUser);

            PhotoUrl = map.get("picurl");
             emmaill=map.get("userEmail");

            UserNam = map.get("userNAME");

            name.setText(UserNam);
            EmailView.setText(emmaill);


        }

        @Override
        public void onCancelled(DatabaseError firebaseError) {

        }
    });

n there is no problem with key n values in database. used the solution but not working

1

5 Answers 5

60

The error points out correctly where you are going wrong

 Map<String, String> map = dataSnapshot.getValue(Map.class);

Map class uses parameter to define the types of Key and Object where as you don't give them and simply use Map.class which fails.

Try the below code - since Key are always string and we can have any type of Object for them

    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
Sign up to request clarification or add additional context in comments.

4 Comments

can you update your question with the new code you used ?
use getValue() you are still doing getValue(Map.class);
See my correct answer down below. as no solution here worked for me
getting this error now java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
34

To introduce the GenericTypeIndicator, you can change this line:

Map<String, String> map = dataSnapshot.getValue(Map.class);

in to this:

GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator );

This should work well in your case. Please give it a try and let me know.

3 Comments

Map<String, String> map = (Map<String, Object>) dataSnapshot.getValue(); should have worked. But in any case, I have updated the answer to show how to use GenericTypeIndicator - you can give this one a try - I hope someone can also find this helpful.
Thanks, this worked whereas the accepted answer did not.
getting this error now com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.lang.Stri
14

I had the same problem and solved it by handling the Object instead trying to have Firebase cast it.

Map <String, String> map = (Map)dataSnapshot.getValue();

did it for me.

1 Comment

Worked for me as well. Nice solution!
2

You need to use the getCheldrin as follows

for (DataSnapshot snapshotNode: dataSnapshot.getChildren()) {
    map.put(snapshotNode.getKey(), snapshotNode.getValue(YOUR_CLASS_TYPE.class));
}

Replace the YOUR_CLASS_TYPE with the class type you expecting. notice that this class must have an empty constructor.

Comments

0

Chage

Map<String, String> map = (Map<String,String>)dataSnapshot.getValue(Map.class);

by this

Map map = (Map) dataSnapshot.getValue(Map.class);

1 Comment

This answer duplicates content from an existing highly rated answer. Please ensure your answers add something more to the post. See How to Answer for more guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.