I am working on a DrawerLayout, and when I click one of the items to go to a Profile page, it logs me out (I can notice this because I get a NullPointer error for the UserAuth [even though I was authed before clicking this]
Note: I didn't notice this until I tried to modularize my code by adding "initialize" methods and such.
This is the code for the items in the drawerlayout:
private void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
//If Clicked...then get item and go to associated page
switch(menuItem.getTitle().toString())
{
case "Profile":
Log.i("Profile", menuItem.getTitle().toString());
startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
/*case "Share":
startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
case "Settings":
startActivity(new Intent(HomeActivity.this, ProfileActivity.class)); */
case "Logout":
Log.i("Logout", menuItem.getTitle().toString());
FirebaseAuth.getInstance().signOut();
//startActivity(new Intent(HomeActivity.this, LoginActivity.class));
}
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
For some reason even though I'm clicking the "Profile" item, it goes to both the Case for that, AND for "Logout" as can be seen by the Log statements:
I/Profile: Profile
I/Logout: Profile
And then this leads to the Error:
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.netgalaxystudios.timeclock/com.netgalaxystudios.timeclock.Activities.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.netgalaxystudios.timeclock.Activities.ProfileActivity.onCreate(ProfileActivity.java:70)
ProfileActivity.java (is the activity I am trying to get to by clicking on the ITEM in the menu). It refers to this line, but really the error is coming from HomeActivity as I understand it... ) :
currentUserString = currentUser.getUid().toString();
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
ImageView menu, closeDrawer;
RelativeLayout navHeader;
//Profile Stuff
DatabaseReference employeesRefPhoto, employeesRefFname, employeesRefLname;
FirebaseDatabase database;
String photoUrl;
ImageView profilePhoto;
FirebaseUser currentUser;
String currentUserString;
TextView nameTV, lnameTV;
TextView businessesTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
/////////////////////////
//METHODS
//initViews();
//setupDrawerLayout();
//footerOnClick();
//////////////////////////
menu = (ImageView) findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
currentUser = FirebaseAuth.getInstance().getCurrentUser();
currentUserString = currentUser.getUid().toString();
setupDrawerLayout();
//https://stackoverflow.com/questions/37163579/null-pointer-exception-on-navigation-header-initialized-variables
navHeader=(RelativeLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(navHeader); //This seems to add a SECOND "X" image
nameTV = (TextView) navHeader.findViewById(R.id.employeeFname);
lnameTV = (TextView) navHeader.findViewById(R.id.employeeLname);
closeDrawer = (ImageView) navHeader.findViewById(R.id.closedrawer);
profilePhoto = (ImageView) navHeader.findViewById(R.id.profilephoto);
businessesTV = (TextView) findViewById(R.id.businesses);
closeDrawer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
drawerLayout.closeDrawer(Gravity.LEFT);
}
});
///////////////////////////////////////////////////////////////////////////
//INFORMATION FOR MENU/////////////////////
//PROFILE PHOTO
//currentUser = FirebaseAuth.getInstance().getCurrentUser();
//currentUserString = currentUser.getUid().toString();
setupDrawerLayout();
database = FirebaseDatabase.getInstance();
employeesRefPhoto = database.getReference("Employees").child(currentUserString).child("photoDownloadUrl");
employeesRefPhoto.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("onDataChange","Inside");
try {
photoUrl = dataSnapshot.getValue().toString();
Log.i("photoUrl", photoUrl);
Glide.with(getApplicationContext()).load(photoUrl).into(profilePhoto);
profilePhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
catch (NullPointerException e) {
Log.i("Null", e.toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Cancelled",databaseError.toString());
}
});
//////////////////////////////////////////////
//GET NAME//////////////////////////////////
//FIRST NAME
employeesRefFname = database.getReference("Employees").child(currentUserString).child("firstName");
employeesRefFname.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
Log.i("firstname", dataSnapshot.getValue().toString());
nameTV.setText(dataSnapshot.getValue().toString());
}
catch (Exception e) { Log.i("FNull?", e.toString()); }
}
@Override
public void onCancelled(DatabaseError databaseError) { Log.d("Cancelled",databaseError.toString()); }
});
//LAST NAME
employeesRefLname = database.getReference("Employees").child(currentUserString).child("lastName");
employeesRefLname.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
Log.i("lastname", dataSnapshot.getValue().toString());
lnameTV.setText(dataSnapshot.getValue().toString());
}
catch (Exception e) { Log.i("LNull?", e.toString()); }
}
@Override
public void onCancelled(DatabaseError databaseError) { Log.d("Cancelled",databaseError.toString()); }
});
//footerOnClick();
footerOnClick();
} //END OF ONCREATE
/////////////////////////////////////////////////////////////////////////////////
//INITIALIZE VIEWS
private void initViews(){
navHeader=(RelativeLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
nameTV = (TextView) navHeader.findViewById(R.id.employeeFname);
lnameTV = (TextView) navHeader.findViewById(R.id.employeeLname);
menu = (ImageView) findViewById(R.id.menu);
closeDrawer = (ImageView) navHeader.findViewById(R.id.closedrawer);
profilePhoto = (ImageView) navHeader.findViewById(R.id.profilephoto);
businessesTV = (TextView) findViewById(R.id.businesses);
}
//INTENTS FOR THE FOOTER OF THE SCREEN
private void footerOnClick() {
businessesTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
startActivity(new Intent(HomeActivity.this, BusinessesActivity.class));
}
});
//EMPLOYEES
//PAYROLL REPORT
}
//MENU / DRAWER
//https://antonioleiva.com/navigation-view/
private void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
//If Clicked...then get item and go to associated page
switch(menuItem.getTitle().toString())
{
case "Profile":
Log.i("Profile", menuItem.getTitle().toString());
startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
/*case "Share":
startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
case "Settings":
startActivity(new Intent(HomeActivity.this, ProfileActivity.class)); */
case "Logout":
Log.i("Logout", menuItem.getTitle().toString());
FirebaseAuth.getInstance().signOut();
//startActivity(new Intent(HomeActivity.this, LoginActivity.class));
}
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
//??????????????????????????????????????
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
} //END OF CLASS