我在处理程序上遇到上述问题,该处理程序每次都会检查您是否登录,但我遇到了该如何解决呢?
我尝试了几种方法,但是我仍然遇到相同的问题,即启动屏幕每次都会检查,因此应用程序崩溃。
这是我的欢迎屏幕
```public class WelcomeScreen extends AppCompatActivity {
private ImageView logo;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private static int SPLASH_TIME_OUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullscreen ();
setContentView (R.layout.welcomescreen);
firebaseAuth = FirebaseAuth.getInstance ();
final String user_id = firebaseAuth.getCurrentUser ().getUid ();
final FirebaseUser firebaseUser = firebaseAuth.getInstance ().getCurrentUser ();
firebaseDatabase = FirebaseDatabase.getInstance ();
final DatabaseReference databaseReference = firebaseDatabase.getReference ().child ("Users").child (user_id);
logo= findViewById (R.id.logoocaap);
Animation animation = AnimationUtils.loadAnimation (this,R.anim.splashscreen);
logo.startAnimation (animation);
new Handler ().postDelayed (new Runnable () {
@Override
public void run() {
//check if there's internet connection
checkConnection();
if(firebaseUser != null)
{
databaseReference.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {```
答案 0 :(得分:1)
首先检查FirebaseUser
是否为null
,然后尝试获取UID
。检查以下内容:
firebaseAuth = FirebaseAuth.getInstance ();
final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String user_id = "";
if(firebaseUser != null)
user_id = firebaseUser.getUid ();
当前,您尝试在UID
的{{1}}上获得FirebaseUser
答案 1 :(得分:0)
更新如下规则
{
"rules": {
".read": true,
".write": true
}
}
然后使用回调函数调用FirebaseAuth.getInstance ().signInAnanymous()
,并在成功方法中获取当前用户
示例:
FirebaseAuth.getInstance ().signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Log.e("TAG", "success sign");
// do your stuff
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("TAG", "failed sign");
}
});
}