[英]Android app problems signout 'boolean com.google.firebase.firestore.DocumentSnapshot.exists()' on a null object reference
I have a problems with my application and I really need your help. 我的应用程序有问题,我真的需要您的帮助。 I am new on Android Studio and I don't know why my apps crashed on SignOut. 我是Android Studio的新手,我不知道为什么我的应用程序在SignOut上崩溃。 I think is because of Query SnapShot and this line of code: 我认为是由于Query SnapShot和以下代码行:
I speak just a little bit in english, so I am very sorry for my poor english :) 我只会说一点英语,所以我为我的英语不好而深感抱歉:)
Here's my code for HomeFragment
这是我的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();
}
}
}
}
});
And for MainActivity I use this only: 对于MainActivity,我只能使用它:
private void logOut() {
mAuth.signOut();
sendToLogin();
}
BlogRecyclerAdapter.java 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);
}
}
});
And my Fatal Error : On BlogRecyclerAdapter but if I remove all code on BlogRecyclerAdapter my apps still crash! 和我的致命错误:在BlogRecyclerAdapter上,但是如果我删除BlogRecyclerAdapter上的所有代码,我的应用程序仍然崩溃! I think the real problems is on HomeFragment! 我认为真正的问题在于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$2.onEvent(BlogRecyclerAdapter.java:117)
at learnorburn.lob_application.BlogRecyclerAdapter$2.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)
Thank you in advance for your help 预先感谢您的帮助
You have error somwehere in your query or db setup. 您的查询或数据库设置中有错误。 Check the documentation of EventListener . 检查EventListener的文档。 It says: 它说:
onEvent will be called with the new value or the error if an error occurred. 如果发生错误,将使用新值或错误调用onEvent。 It's guaranteed that exactly one of value or error will be non-null. 可以保证值或错误之一将正确地为非空。
So in your case if QuerySnapshot documentSnapshots
is null
, it means FirebaseFirestoreException e
is not null
. 因此,在您的情况下,如果QuerySnapshot documentSnapshots
为null
,则意味着FirebaseFirestoreException e
不为null
。 However, you swallow this exception. 但是,您会吞下此异常。 Instead you should log it and check what is written there: 相反,您应该将其记录并检查那里写的内容:
Log.e("MyTag", "Firebase exception", e);
I found a solution but I'm not sure that is a good solution! 我找到了一个解决方案,但是我不确定这是一个好的解决方案! On Firebase Database, I change the rule allow read, write: if request.auth != null; 在Firebase数据库上,我更改了规则“允许读写”:if request.auth!= null; to if true; 如果是真的 and that work. 和那个工作。 I think the problems is because when I logOut, I don't have any permission so my apps crashed. 我认为问题是因为我注销时没有任何权限,所以我的应用程序崩溃了。 Now, I want to know if there's any security or something else who can cause a big problems with this change on Database? 现在,我想知道是否有任何安全性或其他原因会导致数据库的此更改引起大问题? Also thank you very much to help me. 也非常感谢您的帮助。 You don know how i appreciate your help 你不知道我如何感谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.