java - getCurrentUser() 函数错误

标签 java android firebase firebase-authentication

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

当使用 google 进行身份验证时,此代码适用于所有 get 函数,但是当使用 facebook 进行身份验证时,我得到 nullPointerException。 我检查使用此代码的用户

 if (FirebaseAuth.getInstance().getCurrentUser() == null) {
        goToLoginScreen();
    }
    else{
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }

最佳答案

documentation for getCurrentUser()说如果没有当前用户(没有人登录),它实际上会返回 null。您应该始终充分预料到这种情况是可能发生的。

如果您想尽快知道是否有用户登录,您应该使用 AuthStateListener通过在其 onAuthStateChanged() 中调用 getCurrentUser() 来检查登录事件,以查看它是否返回非空值。

关于java - getCurrentUser() 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208917/

相关文章:

ios - 是否可以替代使用 NSNotificationCenter 和 kFIRInstanceIDTokenRefreshNotification 来检测过期 token ?

java - 如何在android中创建一个体积圆

android - MediaPlayer 在旋转时向前跳过约 6 秒

android - 如何使用计时器检查 Corona SDK 中是否发生事件

ios - Swift Firebase 通知 "libc++abi.dylib: terminating with uncaught exception of type NSException"

java - Android - 无法初始化 Google 地方信息

java - 访问自定义异常中的字段时出现问题

java - 将用户输入的字符拆分为 ArrayList

java - getLatitude 和 getLongitude 抛出 NullPointerException

android - 我可以预加载 Animation Drawable 吗?