31

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

Sahaj Rana
  • 1,993
  • 4
  • 25
  • 42
  • None of the answers on this page worked. This answer is the only one that worked for me: https://stackoverflow.com/a/48711195/4833705 – Lance Samaria May 06 '23 at 02:33

5 Answers5

59

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();
Shubhank
  • 21,721
  • 8
  • 65
  • 83
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.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • 1
    `Map map = (Map) 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. – ishmaelMakitla Jun 07 '16 at 20:24
  • Thanks, this worked whereas the accepted answer did not. – RickBoyerDev Jun 01 '17 at 21:16
  • getting this error now com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.lang.Stri – Sunil Chaudhary Mar 06 '19 at 10:47
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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Walter
  • 141
  • 1
  • 2
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.

Hesham Yassin
  • 4,341
  • 2
  • 21
  • 23
0

Chage

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

by this

Map map = (Map) dataSnapshot.getValue(Map.class);
  • This answer duplicates content from an existing highly rated answer. Please ensure your answers add something more to the post. See [answer] for more guidance. – Simon.S.A. Aug 04 '20 at 00:21