我创建了一个名为AddJobHandler的对象。该对象存储用户创建的作业的所有信息。写入数据库效果很好,但是当我在活动开始时尝试从数据库中检索AddJobHandler对象时,出现此错误:
08-10 18:35:46.326 7221-7221/? E/Zygote: v2
08-10 18:35:46.326 7221-7221/? E/Zygote: accessInfo : 0
08-10 18:35:49.013 7221-7221/com.tamir.offen.OddJob E/ViewRootImpl@204300c[LoginActivity]: ViewRootImpl #2 Surface is not valid.
08-10 18:35:49.461 7221-7221/com.tamir.offen.OddJob E/art: The String#value field is not present on Android versions >= 6.0
08-10 18:35:50.198 7221-7221/com.tamir.offen.OddJob E/UncaughtException: com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.tamir.offen.OddJob.map$2.onDataChange(map.java:225)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
08-10 18:35:50.438 7221-7221/com.tamir.offen.OddJob E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tamir.offen.OddJob, PID: 7221
com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.tamir.offen.OddJob.map$2.onDataChange(map.java:225)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
这是我的代码的片段:
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// jobs is an arraylist
jobs.clear();
for(DataSnapshot jobSnapshot : dataSnapshot.getChildren()) {
AddJobHandler job = jobSnapshot.getValue(AddJobHandler.class);
jobs.add(job);
}
for(int i = 0; i < jobs.size(); i++) {
Toast.makeText(map.this, jobs.get(i).getTitle(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
如果您需要更多代码,请随时询问。
编辑:
我试图制作自己的具有默认构造函数以及纬度和经度的类LatLng。我在我的AddJobHandler类中使用了此方法,而不是在Google地图LatLng中使用了该方法,但是我仍然遇到相同的错误。我在做什么错了?
我的LatLng重复班:
public class LatLng {
private double latitude;
private double longitude;
public LatLng() {
}
public LatLng(double lat, double lng) {
latitude = lat;
longitude = lng;
}
public Double getLatitude() {
return latitude;
}
public Double getLongitude() {
return longitude;
}
}
我没有改变从FireBase数据库检索数据的方式。
这是AddJobHandler类,它是Firebase数据库中的存储对象:
public class AddJobHandler{
private static String title, desc, tag, price;
private static List<String> dates = new ArrayList<>(), times = new ArrayList<>();
//private static LatLng location;
private static com.tamir.offen.OddJob.LatLng location;
public AddJobHandler() {
}
public void setTitle(String newTitle) {
title = newTitle;
}
public String getTitle() {
return title;
}
public void setDesc(String newDesc) {
desc = newDesc;
}
public String getDesc() {
return desc;
}
public void setTag(String newTag) {
tag = newTag;
}
public String getTag() {
return tag;
}
public void setPrice(String newPrice) {
price = newPrice;
}
public String getPrice() {
return price;
}
public void setLocation(LatLng newLoc) {
double newLat = newLoc.latitude;
double newLng = newLoc.longitude;
location = new com.tamir.offen.OddJob.LatLng(newLat, newLng);
}
public com.tamir.offen.OddJob.LatLng getLocation() {
return location;
}
public void setDate(String startDate, String endDate) {
dates.clear();
dates.add(startDate); dates.add(endDate);
}
public List<String> getDates() {
return dates;
}
public void setTime(String startTime, String endTime) {
times.clear();
times.add(startTime); times.add(endTime);
}
public List<String> gettime() {
return times;
}
}
我收到此错误:
08-11 14:02:53.618 29198-29198/? E/Zygote: v2
08-11 14:02:53.619 29198-29198/? E/Zygote: accessInfo : 0
08-11 14:02:54.017 29198-29198/com.tamir.offen.OddJob E/art: The String#value field is not present on Android versions >= 6.0
08-11 14:02:54.779 29198-29198/com.tamir.offen.OddJob E/UncaughtException: com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.tamir.offen.OddJob.map$2.onDataChange(map.java:225)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
08-11 14:02:55.018 29198-29198/com.tamir.offen.OddJob E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tamir.offen.OddJob, PID: 29198
com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.tamir.offen.OddJob.map$2.onDataChange(map.java:225)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)