这是日志猫:
java.lang.IllegalArgumentException: method group.onservice.onservice.GetData.setTimestamp argument 1 has type java.sql.Timestamp, got java.util.Date at java.lang.reflect.Method.invoke(Native Method) at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source:134) at com.google.android.gms.internal.zzevb.zza(Unknown Source:437) at com.google.android.gms.internal.zzevb.zza(Unknown Source:2) at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source:9) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:155) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:146) at com.google.firebase.firestore.zzi.onEvent(Unknown Source:16) at com.google.android.gms.internal.zzevc.zza(Unknown Source:6) at com.google.android.gms.internal.zzevd.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是发生错误的代码:
firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
AdPost adPost = doc.getDocument().toObject(AdPost.class);
ad_list.add(adPost);
adRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
我实际上不知道如何编程,所以我在一些论坛和视频中搜索了如何做到这一点(我对编程有点陌生),事实是:我看到每个人都使用相同的代码,并且它对所有人都有效,除了我。因此,没有人报告任何类型的错误。有人可以帮助我理解我的代码有什么问题吗?
PS:指定错误的确切行是这样的:
AdPost adPost = doc.getDocument().toObject(AdPost.class);
AdPost.class:
package group.onservice.onservice;
import java.sql.Timestamp;
public class AdPost {
String des, image_url, titu, user_id, valo;
Timestamp timestamp;
public AdPost() {
}
public AdPost(String des, String image_url, String titu, String user_id, String valo, Timestamp timestamp) {
this.des = des;
this.image_url = image_url;
this.titu = titu;
this.user_id = user_id;
this.valo = valo;
this.timestamp = timestamp;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getTitu() {
return titu;
}
public void setTitu(String titu) {
this.titu = titu;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getValo() {
return valo;
}
public void setValo(String valo) {
this.valo = valo;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}
最佳答案
代码中的问题是您使用的是 Timestamp
对象而不是 Date
对象作为时间戳。 Timestamp
类不是 supported data type在Firestore。为了解决这个问题,我建议你看看我的答案 post ,我在其中解释了如何使用 Date
类将时间戳存储在 Firebase 实时数据库中。您将看到有两种方法,一种使用模型类,另一种使用 map 和 FieldValue.serverTimestamp()
。
关于java - "java.lang.IllegalArgumentException "尝试检索文档数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574112/