Note that the onEvent
method is called with two arguments:
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
name.setText(documentSnapshot.getString("name"));
age.setText(documentSnapshot.getString("age"));
gender.setText(documentSnapshot.getString("gender"));
country.setText(documentSnapshot.getString("country"));
school.setText(documentSnapshot.getString("school"));
}
You're completely ignoring the FirebaseFirestoreException
, which explains the problem.
Most likely you require that the user is signed in, in order for them to be able to read the document. This means that once the user signs out, they lose their permission, and the database rejects the listener. This means your onEvent
gets called again, this time with a FirebaseFirestoreException
instead of a DocumentSnapshot
.
So you should check the error, and only process the document when there was no error:
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if (e != null) {
name.setText(documentSnapshot.getString("name"));
age.setText(documentSnapshot.getString("age"));
gender.setText(documentSnapshot.getString("gender"));
country.setText(documentSnapshot.getString("country"));
school.setText(documentSnapshot.getString("school"));
}
else {
Log.e("Firestore", "Error reading from Firestore", e);
}
}
Troubleshooting NullPointerException
almost always follows the same patter, so I recommend studying What is a NullPointerException, and how do I fix it? to learn how to figure something like this out on your own.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…