Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase LatLng is missing a constructor with no arguments

I am storing coordinates in a realtime database and I am getting an error message when trying to retrieve data. The error message is

com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng is missing a constructor with no arguments

...

at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

Here is the code

Main.java

protected void onStart() {
        apiClient.connect();
        super.onStart();

        DatabaseReference test = database.getReference("jsierra");

        test.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Toast.makeText(getBaseContext(), "Child Added", Toast.LENGTH_LONG).show();
                givenData = dataSnapshot.getValue(data.class); <-- Error
            }
}

data.java

import com.google.android.gms.maps.model.LatLng;
public class data {
    private LatLng coordinates;

    public data() {
        coordinates = null;
    }

    public data(LatLng coordinates)
    {
        this.coordinates = coordinates;
    }

    public void setLatLng(LatLng coordinates) {
        this.coordinates = coordinates;
    }

    LatLng getLatLng () {
        return coordinates;
    }
}
like image 474
Juan Sierra Avatar asked Dec 16 '16 20:12

Juan Sierra


2 Answers

Create your own LatLng model class instead of using the one from Google Maps library because it lacks a public constructor in the implementation.

public class LatLng {
    private Double latitude;
    private Double longitude;

    public LatLng() {}

    ... public getter ...
}

EDIT: how to convert this LatLng to com.google.android.gms.maps.model.LatLng

com.google.android.gms.maps.model.LatLng mapsLatLng = 
    new com.google.android.gms.maps.model.LatLng(latLng.getLatitude(),
                                                 latLng.getLongitude());
like image 115
Wilik Avatar answered Nov 09 '22 20:11

Wilik


I had a similar issue when I tried to pass Timestamp to database. While getter worked and my data were set to realtime database correctly with push, reading the data using the DataSnapshot from ChildEventListener crashed my app with the error because also the Timestamp class has no public cunstructor with no arguement. So I created my own class Timestamp that extended the original java Timestamp class, added the missing public constructor with no arguement.

public class Timestamp extends java.sql.Timestamp {

public Timestamp(long time) {
    super(time);
}

public Timestamp(){
    super(System.currentTimeMillis());

}

I had to then include also the needed setters (as read from warnings when using my new class)

like image 39
Majo Avatar answered Nov 09 '22 20:11

Majo