Can’t Convert Object of type java.lang.String to type

Asked

Viewed 1,251 times

0

I’m trying to get lat e lang which is saved on firebase and recover on map, mPost_key is the uid user , this code works, more than this error when I put mPost_key , we use this same method mDatabase.child(mPost_key) activity to bring other user information and also works

Citation 03-06 19:53:11.270 29642-29642/com.robertoc.meublogclash E/UncaughtException: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.robertoc.meublogclash.User at com.google.android.gms.Internal.zzbtg.zze(Unknown Source) at com.google.android.gms.Internal.zzbtg.zzb(Unknown Source) at com.google.android.gms.Internal.zzbtg.zza(Unknown Source) at com.google.firebase.database.Datasnapshot.getValue(Unknown Source) at com.robertoc.meublogclash.Profile$1.onChildAdded(profile.java:128) at com.google.android.gms.Internal.zzbox.zza(Unknown Source) at com.google.android.gms.Internal.zzbqx.zzZS(Unknown Source) at com.google.android.gms.Internal.zzbra$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:5585) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:730) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:620) 03-06 19:53:11.503 29642-29642/com.robertoc.meublogclash E/Androidruntime: FATAL EXCEPTION: main Process: com.robertoc.meublogclash, PID: 29642 com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type com.robertoc.meublogclash.User at com.google.android.gms.Internal.zzbtg.zze(Unknown Source) at com.google.android.gms.Internal.zzbtg.zzb(Unknown Source) at com.google.android.gms.Internal.zzbtg.zza(Unknown Source) at com.google.firebase.database.Datasnapshot.getValue(Unknown Source) at com.robertoc.meublogclash.Profile$1.onChildAdded(profile.java:128) at com.google.android.gms.Internal.zzbox.zza(Unknown Source) at com.google.android.gms.Internal.zzbqx.zzZS(Unknown Source) at com.google.android.gms.Internal.zzbra$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:5585) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:730) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:620) 03-06 19:53:11.562 1270-1399/? E/GED: Failed to get GED Log Buf, err(0)

mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

     mPost_key = getIntent().getExtras().getString("uidusuario");

     mDatabase.child(mPost_key).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
                User childUser = dataSnapshot.getValue(User.class);
                LatLng childPos = new LatLng(childUser.getLat(), childUser.getLang());
                MarkerOptions markerOptions = new MarkerOptions().position(childPos);
                Marker marker = mMap.addMarker(markerOptions);
                userMarkers.put(dataSnapshot.getKey(), marker);
                userMap.put(dataSnapshot.getKey(), childUser);
            }

            @Override
            public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {


            }

            @Override
            public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

My Clase User

public class User {

private double lat;
private double lang;

public User(){}




public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLang() {
    return lang;
}

public void setLang(double lang) {
    this.lang = lang;
}

inserir a descrição da imagem aqui

I don’t understand why without the mPost_key the code works correctly, in case mPost_key it is for when entering the profile to bring only the position in the uid map of the mPost_key or is a single user.

RESOLVEDThe error was in the database had a number in the middle of the lost path causing the error.

  • The ERROR itself already gives you a clue, "Can’t Convert Object of type java.lang.String"... you are trying to convert an object into a string. Which line is going wrong?

  • 1

    The line where you declare mPost_key it seems to me that you return a string and the code waits User. Any clue?

  • User childUser = dataSnapshot.getValue(User.class); this is the line a Line error

  • mPost_key behind the user uid of fire base

  • I don’t know what was going on but I was doing it the right way, re-did it and it worked.. Thank you who replied!

1 answer

1


The error displayed:

com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type com.robertoc.meublogclash.User at

It means he can’t convert into java.lang.String one com.robertoc.meublogclash.User:

Try to change this line:

User childUser = dataSnapshot.getValue(User.class);

To :

User childUser = dataSnapshot.getValue("ValorUsuario");

Being, ValorUsuario the name of the property!

EDIT

According to the documentation, it is not necessary to inform a parameter:

  User childUser = dataSnapshot.getValue();

getValue() Returns the data contained in this snapshot as native types. Possible types returned are:

  • Boolean
  • String
  • Long
  • Double
  • Map
  • List

This list is recursive; the possible types for Object in the list above are given by the same list. These types match the types available in JSON.

Follows the documentation

  • how do I put Valorusuario = name of the property?

  • these properties are aligned on a certain object? is this property that is the valueUsuario

  • If you leave no parameter inside getValue() of error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.