【问题标题】:" java.lang.IllegalArgumentException " from trying to retrieve document data“java.lang.IllegalArgumentException”试图检索文档数据
【发布时间】:2019-01-05 12:46:19
【问题描述】:

这是日志猫:

java.lang.IllegalArgumentException: 方法 group.onservice.onservice.GetData.setTimestamp 参数 1 具有类型 java.sql.Timestamp,得到 java.util.Date 在 java.lang.reflect.Method.invoke(本机方法) 在 com.google.android.gms.internal.zzevb$zza.zza(未知来源:134) 在 com.google.android.gms.internal.zzevb.zza(未知来源:437) 在 com.google.android.gms.internal.zzevb.zza(未知来源:2) 在 com.google.firebase.firestore.DocumentSnapshot.toObject(未知 来源:9) 在 group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:155) 在 group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:146) 在 com.google.firebase.firestore.zzi.onEvent(未知来源:16) 在 com.google.android.gms.internal.zzevc.zza(未知来源:6) 在 com.google.android.gms.internal.zzevd.run(未知来源:6) 在 android.os.Handler.handleCallback(Handler.java:790) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6494) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) 在 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;
    }

}

【问题讨论】:

  • 请添加你的AdPost类的代码。
  • 好的,现在添加。

标签: java android firebase timestamp google-cloud-firestore


【解决方案1】:

您的代码中的问题是您使用的是Timestamp 对象而不是Date 对象。 Timestamp 类不是 Firestore 中的 supported data type。为了解决这个问题,我建议您从 post 中查看我的答案,其中我解释了如何使用 Date 类将时间戳存储在 Firebase 实时数据库中。您会看到有两种方法,一种使用模型类,另一种使用地图和FieldValue.serverTimestamp()

【讨论】:

  • 是的,就是这样。谢谢,我已经放弃了我的项目。
猜你喜欢
  • 2019-03-31
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多