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;
}
}
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());
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With