Ok, so i have been looking around for a solution to this problem and all the solutions I have found are not working. I am trying to change the text of my Sign In Button in my navigationView to Sign Out/Sign In depending on if they are logged in or not and then sending them off to sign in or out if they press the button. For some reason my app is crashing with a fatal exception on a null object reference(some of the Log below). What am I doing wrong?
I created "signInOutText();" so i could try placing it in different spots, it is currently in onResume but i have tried in onCreate as well and originally had it all in onResume. Hopefully I am just overlooking something simple. I cropped out some of my code from MainActivity.java below so it was not so long but let me know if you need to see the full thing.
MainActivity.java
public class MainActivity extends AppCompatActivity {
//Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
String category;
MenuItem mPreviousMenuItem;
private ProgressDialog mProgressDialog;
String providerId,uid,name,email;
Uri photoUrl;
private static final String TAG = "MainActivity";
// set name of your preferences list
private static String MY_PREFERENCES = "Settings";
String userNameLoad, emailLoad, profileLoad;
TextView usernameTextView, emailTextView, signInTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setCheckable(true);
menuItem.setChecked(true);
if (mPreviousMenuItem != null) {
mPreviousMenuItem.setChecked(false);
}
mPreviousMenuItem = menuItem;
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with fragments
case R.id.home:
//code for button
break;
case R.id.cat1:
//code for button
break;
case R.id.signIn:
signInOut();
break;
default:
break;
}
return true;
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
private void signInOutText() {
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Get header xml from navigationView
View header = navigationView.getHeaderView(0);
//Check if user logged in, change sign in sign out button to correct text
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
signInTextView = (TextView) header.findViewById(R.id.signIn);
signInTextView.setText(R.string.sign_out);
} else {
signInTextView = (TextView) header.findViewById(R.id.signIn);
signInTextView.setText(R.string.sign_in);
}
}
private void signInOut(){
//TODO add Code .... if logged in logout
Intent intent = new Intent(MainActivity.this, ChooserActivity.class);
startActivity(intent);
finish();
}
public void setActionBarTitle(String title) {
if(getSupportActionBar() != null){
getSupportActionBar().setTitle(title);
}
}
@Override
public void onResume() {
super.onResume();
// Set title
setActionBarTitle(getString(R.string.app_name));
signInOutText();
}
}
Log
Unable to resume activity {com.test.app/com.test.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(int)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4156)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3361)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at com.test.app.MainActivity.signInOutText(MainActivity.java:203)
at com.test.app.MainActivity.onResume(MainActivity.java:307)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1286)
at android.app.Activity.performResume(Activity.java:6987)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4145)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3361)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
menu/drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/drawer_group"
android:checkable="true">
<item
android:id="@+id/home"
android:checked="false"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/home_string" />
</group>
<group android:id="@+id/group_cat"
android:checkable="true">
<item
android:id="@+id/menuCategories"
android:title="@string/categories_string"
android:layout_gravity="bottom">
<menu>
<item
android:id="@+id/cat1"
android:checked="false"
android:icon="@drawable/ic_inbox_black"
android:title="@string/cat1_string" />
</menu>
</item>
</group>
<group android:id="@+id/group_signInOut"
android:checkable="true">
<item
android:id="@+id/signIn"
android:checked="false"
android:icon="@drawable/ic_power_settings_new_black_24dp"
android:title="@string/sign_in" />
</group>
</menu>
Thanks for the help!
Ok, so I realized that I was trying to update a menu "item" and not a textView so that made a big difference. Here is the code I used in my signInOutText() to get it to work:
private void signInOutText() {
// get menu from navigationView
Menu menu = navigationView.getMenu();
// find MenuItem you want to change
MenuItem nav_signIn = menu.findItem(R.id.signIn);
//Check if user logged in, change sign in/out button to correct text
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
nav_signIn.setTitle(R.string.sign_out);
} else {
nav_signIn.setTitle(R.string.sign_in);
}
}
I hope it helps someone! Thanks for the help!