Java 两个活动之间的Android studio Firestore时间戳错误
我有一个简单的建议页面,我可以在其中键入标题和内容,然后将一些其他信息存储到Firestore,并在ListView页面上显示。它本身工作正常,但在我发送它之后,会弹出一个错误,它显示错误是listview页面上的toDate时间戳 活动顺序为listview>发送页面>listviewJava 两个活动之间的Android studio Firestore时间戳错误,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我有一个简单的建议页面,我可以在其中键入标题和内容,然后将一些其他信息存储到Firestore,并在ListView页面上显示。它本身工作正常,但在我发送它之后,会弹出一个错误,它显示错误是listview页面上的toDate时间戳 活动顺序为listview>发送页面>listview //the send activity send.setOnClickListener(new View.OnClickListener() { @Override
//the send activity
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getId();
String title = art1.getText().toString();
String content = art2.getText().toString();
Intent intent = new Intent(create_announce_main.this, SuggestionMain.class);
// DocumentReference documentReference = firebaseFirestore.collection("announce").document("ann");
Map<String, Object> suggest = new HashMap<>();
suggest.put("title", title);
suggest.put("content", content);
suggest.put("name", name);
suggest.put("recID", recID);
suggest.put("admin_name", "");
suggest.put("response_content", "");
suggest.put("response_status", "未回覆");
suggest.put("response_time",FieldValue.serverTimestamp());
suggest.put("createdAt", FieldValue.serverTimestamp());
firebaseFirestore.collection("Suggestion").document().set(suggest).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(create_announce_main.this, "added succesfully", Toast.LENGTH_LONG).show();
}
}
});
startActivity(intent);
}
});
当我第一次触摸firestore时,我对这类事情有了印象,所以我尝试先开始发送活动,而不是视图,它工作了,但只有一次,第二次它显示相同的错误,我尝试在单击sendpage时完成整个活动列表视图,但仍然不工作
Firestore正在完美地获取数据,重新启动应用程序后,listview显示数据,因此该功能仍在运行。
由于以下代码行,您将获得NullPointerException:
Date date = timestamp.toDate();
这是因为timestamp
对象位于:
Timestamp timestamp = (Timestamp) snapshot.getData().get("createdAt");
具有null的值。要解决此问题,请将上述代码行更改为:
if (snapshot.getDate("createdAt") != null) {
Date timestamp = snapshot.getDate("createdAt");
//Do what you need to do with this timestamp
}
除此之外,类似这样的查询:
firebaseFirestore.collection("Suggestion").whereEqualTo("recID",recID).orderBy("createdAt", Query.Direction.DESCENDING)
需要索引。要添加这样的索引,请查看我在以下帖子中的答案:
recID
的值是多少,请向我们显示更详细的数据库结构,包括您的建议集合。集合添加您已添加用户集合,还请添加建议
集合。我已经添加了一个索引,感谢您的回答,它发现加载数据的速度比预期的慢了一点,因此它捕获除值之外的空值。是否有解决方案?或者它每次都需要非空函数是的,它不应该为空,因此进行空检查。
if (snapshot.getDate("createdAt") != null) {
Date timestamp = snapshot.getDate("createdAt");
//Do what you need to do with this timestamp
}
firebaseFirestore.collection("Suggestion").whereEqualTo("recID",recID).orderBy("createdAt", Query.Direction.DESCENDING)