问题
I am trying to fetch the FirebaseLocationData object from Firebase Realtime Database in Android.
According to Docs, the class should have no arguments constructor which i have done as
public FirebaseLocationData() {} but it is still showing error
com.google.firebase.database.DatabaseException: Class android.location.Location does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
How am i supposed to make a no-argument constructor of android.location.Location class?
Error is produced by this line FirebaseLocationData fld = dataSnapshot.getValue(FirebaseLocationData.class);
code:
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
FirebaseLocationData fld = dataSnapshot.getValue(FirebaseLocationData.class);
Toast.makeText(MainActivity.this, fld.getEmail()+" added", Toast.LENGTH_SHORT).show();
}
FirebaseLocationData class:
public class FirebaseLocationData {
String email;
Location location;
String time;
public FirebaseLocationData() {
}
public FirebaseLocationData(Location location, String email, String time) {
this.location = location;
this.email = email;
this.time = time;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
回答1:
You're trying to write a android.location.Location object and that class doesn't have a public, no-argument constructor. Many Android library classes will not meet the requirements from Firebase, and you'll have to write your own serialization/deserialization to exchange the necessary information with the database.
Also see: Class android.location.Location does not define a no-argument constructor
来源:https://stackoverflow.com/questions/50894716/how-to-add-no-argument-constructor-of-inbuilt-class-for-firebase-database-except