Android 应用在空对象引用上出现注销问题 'boolean com.google.firebase.firestore.DocumentSnapshot.exists()'

Android app problems signout 'boolean com.google.firebase.firestore.DocumentSnapshot.exists()' on a null object reference

我的申请有问题,我真的需要你的帮助。我是 Android Studio 的新手,我不知道为什么我的应用程序在注销时崩溃。我认为是因为 Query SnapShot 和这行代码:

我只会说一点英语,所以我很抱歉我的英语不好:)

这是我的 HomeFragment

代码
 Query firstQuery =
 firebaseFirestore.collection("Posts").orderBy("timestamp",
 Query.Direction.DESCENDING).limit(3);
         firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
             @Override
             public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                 if (!documentSnapshots.isEmpty()) {
                     if (isFirstPageFirstLoad) {
                         lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                         blog_list.clear();
                     }

                     for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                         if (doc.getType() == DocumentChange.Type.ADDED) {

                             String blogPostId = doc.getDocument().getId();
                             BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);

                             if (isFirstPageFirstLoad) {
                                 blog_list.add(blogPost);
                             } else {
                                 blog_list.add(0, blogPost);
                             }
                             blogRecyclerAdapter.notifyDataSetChanged();
                         }
                     }
                     isFirstPageFirstLoad = false;
                 }
             }
         });
     }
     // Inflate the layout for this fragment
     return view;
 }

 public void loadMorePost(){
     if(firebaseAuth.getCurrentUser() != null) {
         final Query nextQuery = firebaseFirestore.collection("Posts")
                 .orderBy("timestamp", Query.Direction.DESCENDING)
                 .startAfter(lastVisible)
                 .limit(3);

         nextQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
             @Override
             public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                 if (!documentSnapshots.isEmpty()) {
                     lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                     for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                         if (doc.getType() == DocumentChange.Type.ADDED) {
                             String blogPostId = doc.getDocument().getId();
                             BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                             blog_list.add(blogPost);
                             blogRecyclerAdapter.notifyDataSetChanged();
                         }
                     }
                 }
             }
         });

对于 MainActivity,我只使用它:

private void logOut() {
    mAuth.signOut();
    sendToLogin();
}

BlogRecyclerAdapter.java

    firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").addSnapshotListener( new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if(!documentSnapshots.isEmpty()){
                int count = documentSnapshots.size();
                holder.updateLikesCount(count);

            } else {
                holder.updateLikesCount(0);
            }
        }
    });

还有我的致命错误:在 BlogRecyclerAdapter 上,但是如果我删除 BlogRecyclerAdapter 上的所有代码,我的应用程序仍然会崩溃!我认为真正的问题出在 HomeFragment 上!

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: learnorburn.lob_application, PID: 15909
                  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.firestore.QuerySnapshot.isEmpty()' on a null object reference
                      at learnorburn.lob_application.BlogRecyclerAdapter.onEvent(BlogRecyclerAdapter.java:117)
                      at learnorburn.lob_application.BlogRecyclerAdapter.onEvent(BlogRecyclerAdapter.java:113)
                      at com.google.firebase.firestore.zzi.onEvent(Unknown Source)
                      at com.google.android.gms.internal.zzevc.zza(Unknown Source)
                      at com.google.android.gms.internal.zzevd.run(Unknown Source)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1510)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1400)

提前感谢您的帮助

您的查询或数据库设置中的某个地方有错误。查看 EventListener 的文档。它说:

onEvent will be called with the new value or the error if an error occurred. It's guaranteed that exactly one of value or error will be non-null.

所以在你的情况下,如果 QuerySnapshot documentSnapshotsnull,则意味着 FirebaseFirestoreException e 不是 null。但是,您吞下了这个异常。相反,你应该记录它并检查那里写的是什么:

Log.e("MyTag", "Firebase exception", e);

我找到了解决方案,但我不确定这是一个好的解决方案! 在 Firebase 数据库上,我更改了允许读取、写入的规则:if request.auth != null;如果为真;那工作。 我认为问题是因为当我注销时,我没有任何权限,所以我的应用程序崩溃了。现在,我想知道是否有任何安全性或其他因素会导致数据库的此更改出现大问题? 也非常感谢你帮助我。你不知道我多么感激你的帮助