Home > OS >  FATAL ERROR com.google.firebase.database.DatabaseException: Failed to convert a value of type java.l
FATAL ERROR com.google.firebase.database.DatabaseException: Failed to convert a value of type java.l

Time:11-24

I am getting this error and I can't find the reason. I try to retrieve lat and long from Firebase. Here its my code from Java class:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {

    private GoogleMap mMap;
    FirebaseFirestore db;
    private DatabaseReference mUsers;
    private ChildEventListener mChildEventListener;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // initializing our firebase firestore.
        db = FirebaseFirestore.getInstance();

        // Obtain the SupportMapFragment and get
        // notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        ChildEventListener mChildEventListener;
        mUsers = FirebaseDatabase.getInstance().getReference("Locations");
        mUsers.push().setValue(marker);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setOnMarkerClickListener(this);
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mUsers.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.exists()) {
                    for (DataSnapshot dataSnapshot:snapshot.getChildren()) {
                        MapAdapter mapAdapter = dataSnapshot.getValue(MapAdapter.class);
                        LatLng location = new LatLng(mapAdapter.latitude, mapAdapter.longitude);
                        mMap.addMarker(new MarkerOptions().position(location)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }
}

Here is my code from the adapter:

public class MapAdapter {
     public Long latitude,longitude ;

    public MapAdapter() {

    }

    public Long getLatitude() {
        return latitude;
    }

    public void setLatitude(Long latitude) {
        this.latitude = latitude;
    }

    public Long getLongitude() {
        return longitude;
    }

    public void setLongitude(Long longitude) {
        this.longitude = longitude;
    }
}

enter image description here

To solve this, you either change the type of the fields in the database to be numbers and you leave the code as it is, or you change the MapAdapter class like this:

public class MapAdapter {
    public String latitude, longitude;

    public MapAdapter() {}

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}

And you also change the code like this:

MapAdapter mapAdapter = dataSnapshot.getValue(MapAdapter.class);
long latitude = Long.parseLong(mapAdapter.latitude);
long longitude = Long.parseLong(mapAdapter.longitude);
LatLng location = new LatLng(latitude, longitude);
  • Related