我正在嘗試在 Cloud Firestore 中添加文檔,但我需要用于timestamp添加文檔的服務器。文檔已成功添加到集合中,并帶有正確的timestamp. 我正在添加帶有警告對話框的文檔。但是當我返回到我的活動應用程序時,它會因以下錯誤而終止:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException ....... E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Date com.google.firebase 原因: .Timestamp.toDate()' 在 com.sasam.virtuallibrary.Models.UserLight.setTimestamp(UserLight.java:103) 的空對象引用上為了顯示文檔,我正在使用以下查詢Query query= FirebaseFirestore.getInstance() .collection("Users") .document(user.getUid()) .collection("friends") .orderBy("timestamp", Query.Direction.DESCENDING);我的課在這里public class UserLight{ protected String name; protected String email; protected String uid; protected Float rating; protected String photoUrl; protected long timestamp; public UserLight(){ } //other getters and setters @Exclude public long getTimestampLong(){ return timestamp; } public FieldValue getTimestamp() { return FieldValue.serverTimestamp(); } public void setTimestamp(Timestamp timestamp) { this.timestamp = timestamp.toDate().getTime(); }}
在 Firestore 中獲取服務器時間戳時出錯
慕尼黑8549860
2023-06-14 16:28:16