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
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.
Comments