“ java.lang.IllegalArgumentException”,尝试检索文档数据

时间:2018-07-28 18:28:11

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

这是logcat:

  

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;
    }

}

1 个答案:

答案 0 :(得分:0)

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