0

So in my code I had created sign-in page where it sees to which node is the user connected(driver or customer ) and takes them accordingly to that page and It also keeps the user signed in even after they user closes the app but whenever I try to logout from the page the app crashes this is my login page

public class Login_Page extends AppCompatActivity {
    ActivityLoginPageBinding binding;
    ProgressDialog progressdialog;
    FirebaseAuth auth;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginPageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        button = findViewById(R.id.Googlelogin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login_Page.this, SignUp.class);
                startActivity(intent);
            }
        });
        progressdialog = new ProgressDialog(Login_Page.this);
        progressdialog.setTitle("Logging in");
        progressdialog.setMessage("Please wait");
        auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() != null) {
            String uid = auth.getInstance().getCurrentUser().getUid();
            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
            DatabaseReference uidRef = rootRef.child("Users").child("Driver").child(uid);
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        Toast.makeText(Login_Page.this, "LoginDriver", Toast.LENGTH_SHORT);
                        Intent intent = new Intent(Login_Page.this, HomePage.class);
                        startActivity(intent);
                    } else {
                        startActivity(new Intent(Login_Page.this, Custhomepage.class));
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            };
            uidRef.addListenerForSingleValueEvent(valueEventListener);
        } else {
            binding.Login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    progressdialog.show();
                    auth.signInWithEmailAndPassword(binding.Email.getText().toString(), binding.Password.getText().toString())
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    progressdialog.dismiss();
                                    if (task.isSuccessful()) {
                                        Toast.makeText(Login_Page.this, "Success", Toast.LENGTH_SHORT).show();
                                        String uid = auth.getInstance().getCurrentUser().getUid();
                                        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                                        DatabaseReference uidRef = rootRef.child("Users").child("Driver").child(uid);
                                        ValueEventListener valueEventListener = new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                if (snapshot.exists()) {
                                                    Toast.makeText(Login_Page.this, "LoginDriver", Toast.LENGTH_SHORT);
                                                    Intent intent = new Intent(Login_Page.this, HomePage.class);
                                                    startActivity(intent);
                                                } else {
                                                    startActivity(new Intent(Login_Page.this, Custhomepage.class));
                                                }
                                            }

                                            @Override
                                            public void onCancelled(@NonNull DatabaseError error) {

                                            }
                                        };
                                        uidRef.addListenerForSingleValueEvent(valueEventListener);
                                    } else {
                                        Toast.makeText(Login_Page.this, task.getException().getMessage(), Toast.LENGTH_SHORT);
                                    }
                                }
                            });

                }
            });


        }
    }
}

and this is part in the homepage of both customer and driver from where the user can logout

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.setting:
                break;

            case R.id.logout:
                auth.signOut();
                Intent intent=new Intent(HomePage.this,Login_Page.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

Edits: when the app crashes i get this in my Logcat

2021-06-22 11:22:59.829 10882-10882/com.example.deliveryapp E/InputEventReceiver: Exception dispatching input event.
2021-06-22 11:22:59.829 10882-10882/com.example.deliveryapp E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2021-06-22 11:22:59.834 10882-10882/com.example.deliveryapp E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.signOut()' on a null object reference

Also I didn't have this problem when I only had 1 home page but when I added the code to keep user signed in to the where they are suppose to go (basically when I added 1 more home page ) I started getting problem in logout

1 Answers1

1

Looks like, you have not initialized auth object before calling auth.signout(). That could be the reason for the exception. Call the below logout() method in your case R.id.logout:

private void logout () {
    FirebaseAuth mAuth = FirebaseAuth.getInstance ();
    mAuth.signOut ();
    finish ();
    startActivity ( new Intent ( this, Login_Page.class ) );
}

Hope the above code snippet helps you!

niranj1997
  • 719
  • 8
  • 16