本文介绍了getCurrentUser()函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
currentFirebaseUser.getDisplayName();

当与Google进行身份验证时,此代码可与所有get函数一起使用,但与Facebook进行身份验证时,我将获得nullPointerException.我使用此代码检查用户

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();
    }

推荐答案

getCurrentUser()的文档说,实际上,如果没有当前用户(没有人登录),它将返回null.您应该始终完全希望这种情况是有可能的.

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.

如果您想尽快了解是否有用户登录,则应该使用 AuthStateListener 通过在其onAuthStateChanged()中调用getCurrentUser()以查看其是否返回非null来检查登录事件.

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.

这篇关于getCurrentUser()函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 02:41