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());
例外:
由于:java.lang.NullPointerException:尝试调用虚拟 方法'java.lang.String com.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.
解决方案是仅在存在登录的yser的情况下才调用getUid()
:
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屋!