My app crashes when I hit log out

Seamy20

I have an app that allows me to log in and out. the log out button now seems to crash the app when i press it when it was working ok before. It still logs out the user and when you go back into the app you have to sign in again.

I have added my whole on create method below where the action happens at the bottom. Just incase i have added a duplicate or something that i am missing

 //view objects
    private TextView textViewUserEmail;
    private Button buttonLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        buttonAddProducts = (Button) findViewById(R.id.buttonAddProducts);
        buttonAddBeverages = (Button) findViewById(R.id.buttonAddBeverages);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);
        firebaseAuth = FirebaseAuth.getInstance();
        buttonAddProducts.setVisibility(View.GONE);
        buttonAddBeverages.setVisibility(View.GONE);

        databaseUsers = FirebaseDatabase.getInstance().getReference("users");

        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference currentUserReference = databaseUsers.child(uid);
        currentUserReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot userSnapshot) {
                if (userSnapshot.exists()) {
                    User user = userSnapshot.getValue(User.class);

                    if(user.getAdminUser() == true)
                    {
                        buttonAddProducts.setVisibility(View.VISIBLE);
                        buttonAddBeverages.setVisibility(View.VISIBLE);
                    }
                }
                else{
                    Log.i("myInfoTag", "Connection problem");
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException(); // don't ignore exceptions
            }
        });


        //if the user is not logged in
        //that means current user will return null
        if(firebaseAuth.getCurrentUser() == null){
            //closing this activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }

        //getting current user
        FirebaseUser user = firebaseAuth.getCurrentUser();

        //initializing views
        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);


        //displaying logged in user name
        textViewUserEmail.setText("Welcome "+user.getEmail());


        buttonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //logging out the user
                firebaseAuth.signOut();
                //closing activity
                finish();
                //starting login activity
                startActivity(new Intent(ProfileActivity.this, SignUpActivity.class));
            }
        });


    }

When i press log out it just crashes with the msg beer app has stopped

Bob Snyder

The following block of code does not cause an immediate transfer of execution to the started activity. It only initiates that flow, and execution continues to the following statements.

    if(firebaseAuth.getCurrentUser() == null){
        //closing this activity
        finish();
        //starting login activity
        startActivity(new Intent(this, LoginActivity.class));
    }

In your case, one of the following statements is user.getEmail(), which will throw a NullPointerException when there is no user signed in.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My Android App crashes when I hit the button

My app crashes when I put extras on an Intent

My app crashes when the device is in airplane mode

My first Android app crashes when rotated

My app crashes when opening tablayout activity

My app crashes when i press the start button

My App Crashes when calling my Animation?

My app crashes when I try to add Image Button to it

My SQLite database is not created when i open the app and it crashes

Android app crashing when i hit a button

My app crashes when I place the 4th column in my databases's query

my app crashes when I try to download an image

My app crashes when I hit BACK button on one of my activities

my app crashes when I start a new intent

when i hit play button more then once in my mp3 player my application crashes

i want to make custom toolbar but when i open my app my app crashes

My Android app only crashes when not debuggable?

my app crashes when clicking the submit button

My Whole App Crashes When I Click My Play Again Button

App crashes when I click the search button in my other fragments

Why my app crashes when I put the recycler view?

It crashes my app when I put onClickListener on a ImageView

How can I find out why my app crashes if logcat show no reason?

my app crashes "sometimes" when i press delete button

My app crashes when i try to move to a particular layout

crashes app when I log in using facebook auth

App crashes when i try to add data to my database

Android app crashes when seekbar maxed out

I got this error when hit the delete icon on my react app