Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead

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

like image 291
Sahaj Rana Avatar asked Jun 07 '16 19:06

Sahaj Rana


3 Answers

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();
like image 169
Shubhank Avatar answered Nov 03 '22 04:11

Shubhank


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.

like image 34
ishmaelMakitla Avatar answered Nov 03 '22 03:11

ishmaelMakitla


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.

like image 14
Walter Avatar answered Nov 03 '22 02:11

Walter