FirebaseAuth.getCurrentUser() 返回空值 [英] FirebaseAuth.getCurrentUser() is returning null value
问题描述
FirebaseAuth.getCurrentUser()
正在返回 null
值.以前我的代码运行良好,但几天后它返回 currentUser
作为 null
.
FirebaseAuth.getCurrentUser()
is returning null
value. Previously my code worked fine but since couple of days its returning currentUser
as null
.
此外,我在另一台设备上试用了我的应用程序,在该设备上使用相同的代码一切正常.
Also I've tried my app on another device and in that device everything is working fine with the same code.
mAuth=FirebaseAuth.getInstance();
mCurrentUser=mAuth.getCurrentUser()
mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users")
.child(mCurrentUser.getUid());
异常:
Caused by: java.lang.NullPointerException: Attempt to invoke方法'java.lang.Stringcom.google.firebase.auth.FirebaseUser.getUid()' 在一个空对象上参考
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
我已经尝试在我的设备上擦除应用数据并卸载并重新安装我的应用,但没有任何效果.
I have already tried erasing app data and uninstalling and reinstalling my app on my device but nothing worked.
我的设备或 Firebase
有问题吗?
Is there a problem with my device or Firebase
?
推荐答案
看起来用户未登录到您的应用,这会导致 mCurrentUser.getUid()
抛出异常.
It looks like the user isn't signed into your app, which causes mCurrentUser.getUid()
to throw an exception.
解决方案是只调用 getUid()
如果有登录的 yser:
The solution is to only call getUid()
is there is a signed-in yser:
mCurrentUser=mAuth.getCurrentUser();
if (mCurrentUser != null) {
mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users")
.child(mCurrentUser.getUid());
}
这篇关于FirebaseAuth.getCurrentUser() 返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!