如何用文本框中的值替换文档路径?

时间:2019-02-08 08:24:54

标签: android firebase google-cloud-firestore

我尝试使用文本框中的值设置文档路径。 但是,只要我尝试执行此应用,应用就会继续崩溃。

    DocumentReference docRef = db.collection("Users").document(textDisplay2.getText().toString());
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null && document.exists()) {
                Log.d("TAG", document.getString("name")); //Print the name
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
      }
    });

1 个答案:

答案 0 :(得分:0)

DocumentReference docRef = db.collection("Users").document(String.valueOf(textDisplay2.getText());
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document != null && document.exists()) {
            Log.d("TAG", document.getString("name")); //Print the name
        } else {
            Log.d(TAG, "No such document");
        }
    } else {
        Log.d(TAG, "get failed with ", task.getException());
    }
  }
});