0

I am facing the above problem on my handler which checks every time if your logged in but I am facing that problem how can I solve that?

Guys I have tried several methods but I am still getting the same problem it's the splash screen which checks every time so, the app is getting crash.

Here is my welcome screen

```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) {```
Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
Kyle Mutta
  • 379
  • 1
  • 5
  • 16

2 Answers2

1

First check whether FirebaseUser is null or not then try to get UID. Check below:

firebaseAuth = FirebaseAuth.getInstance ();
final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String user_id = "";

if(firebaseUser != null)
    user_id = firebaseUser.getUid ();

Currently you try to get UID on FirebaseUser which is null

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

Enable signin method in your firebase console for your testing enable signinAnanymous picture

Update rules like below

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Then call FirebaseAuth.getInstance ().signInAnanymous() with callback's and get current user inside success method

Example :

 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");
                    }
                });
    }
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30