问题
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
currentFirebaseUser.getDisplayName();
this code works with all get functions when auth with google but when auth with facebook i get nullPointerException. i check for user using this code
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
goToLoginScreen();
}
else{
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
}
回答1:
The documentation for getCurrentUser() says that it will, in fact, return null if there is no current user (there is no one logged in). You should always fully expect this case to be a possibility.
If you want to know as soon as possible if there is a user logged in, you should instead use an AuthStateListener to check for a login event by calling getCurrentUser() in its onAuthStateChanged() to see if it returns non-null.
来源:https://stackoverflow.com/questions/46208917/getcurrentuser-functions-error