0

由于我将 Firestore 规则更改为

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

当我退出时,我收到了这个错误。

2020-05-03 15:53:01.337 28023-28023/com.wordpress.asayerbh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wordpress.asayerbh, PID: 28023
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.firestore.DocumentSnapshot.exists()' on a null object reference
        at com.wordpress.asayerbh.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:111)
        at com.wordpress.asayerbh.-$$Lambda$MainActivity$Ar1zoAyUIQ5aSR4DFFSn0UMdmmM.onEvent(Unknown Source:4)
        at com.google.firebase.firestore.DocumentReference.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.4.3:482)
        at com.google.firebase.firestore.DocumentReference$$Lambda$3.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.4.3:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我在导航抽屉标题中包含用户名的主要活动代码。

if (user.getDisplayName() == null || user.getDisplayName().equals("")) {

   DocumentReference documentReference = mStore.collection("users").document(userID);
    documentReference.addSnapshotListener(this, (documentSnapshot, e) -> {
        if(documentSnapshot.exists()){
          nav_user.setText(documentSnapshot.getString("fName"));
      } else {
         Log.d("tag", "onEvent: Document do not exists");
       }
  });
}

我搜索了一下。我发现了有关文档快照的问题。并应删除以进行注销。怎么做?

4

1 回答 1

1

我找到了这个解决方案documentSnapshot != null && documentSnapshot.exists())

DocumentReference documentReference = mStore.collection("users").document(userID);
                documentReference.addSnapshotListener(this, (documentSnapshot, e) -> {
                    if (documentSnapshot != null && documentSnapshot.exists()) {
                        nav_user.setText(documentSnapshot.getString("fName"));
                    } else {
                        Log.d("tag", "onEvent: Document do not exists");
                    }
                });
于 2020-05-03T13:30:54.633 回答