Please could someone help me understand why I am getting an error here. I am still fairly new to java and Android.
I have a drawer layout that opens and I specify a different heading in a textview depending if a person is logged in or not. For some reason I intermittently get a NullPointerException error when the setText argument is run on my textview. Here is the code for the class:
public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
// I am declaring the DrawerLayout here so I can open it if logged in later
private DrawerLayout drawer;
private boolean firstResume = false;
private String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Loading the Google login screen", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startActivity(new Intent(NavigationActivity.this, LoginActivity.class));
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// this is so it doesn't open every time you swap applications ....
if(savedInstanceState == null){
firstResume = true;
}
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// Make sure the correct name is being displayed whenever opened
TextView tvuser = (TextView) findViewById(R.id.nav_header_name);
ImageView ivuser = (ImageView) findViewById(R.id.imgProfilePic2);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
username = preferences.getString("username", "");
Uri userpic = Uri.parse(preferences.getString("userpic", ""));
String welcome = getString(R.string.welcome_msg, username);
if (!username.equalsIgnoreCase("")) {
tvuser.setText(welcome);
if (userpic != null) {
ivuser.setVisibility(View.VISIBLE);
GlideApp
.with(getApplicationContext())
.load(userpic)
.into(ivuser);
}
} else {
tvuser.setText("Please log in using the button on the bottom right");
ivuser.setVisibility(View.INVISIBLE);
}
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// This is used to load the animation
ImageView mImageViewStatsAnimation = (ImageView) findViewById(R.id.animation_list_background);
((AnimationDrawable) mImageViewStatsAnimation.getBackground()).start();
}
// I want to drawer to open if the user is logged in
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
username = preferences.getString("username", "");
if(firstResume && !username.equalsIgnoreCase("")) {
drawer.openDrawer(Gravity.LEFT);
}
firstResume = false;
}
The error happens at either:
tvuser.setText("Please log in using the button on the bottom right");
or at:
tvuser.setText(welcome);
Here is the relevant output of the debugger:
V/InputMethodManager: START INPUT: android.support.design.internal.NavigationMenuView{42284ad8 VFED.V.. .F...... 0,0-960,527 #7f0e00c0 app:id/design_navigation_view} ic=null tba=android.view.inputmethod.EditorInfo@43214a00 controlFlags=#100
D/AndroidRuntime: Shutting down VM
D/dalvikvm: threadid=1: detach (group=0x41d6dce0)
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d6dce0)
W/dalvikvm: threadid=1: uncaught exception occurred
W/System.err: java.lang.NullPointerException
W/System.err: at za.co.psychstat.learningrstats.NavigationActivity$2.onDrawerOpened(NavigationActivity.java:82)
W/System.err: at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:828)
W/System.err: at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:773)
W/System.err: at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:2077)
W/System.err: at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:880)
W/System.err: at android.support.v4.widget.ViewDragHelper$2.run(ViewDragHelper.java:337)
W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
W/System.err: at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5299)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
E/AndroidRuntime: FATAL EXCEPTION: main
Process: za.co.psychstat.learningrstats, PID: 3114
java.lang.NullPointerException
at za.co.psychstat.learningrstats.NavigationActivity$2.onDrawerOpened(NavigationActivity.java:82)
at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:828)
at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:773)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:2077)
at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:880)
at android.support.v4.widget.ViewDragHelper$2.run(ViewDragHelper.java:337)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm: threadid=11: exiting
D/dalvikvm: threadid=11: detach (group=0x41d6dce0)
D/dalvikvm: threadid=11: removing from list
D/dalvikvm: threadid=11: bye!
I/Process: Sending signal. PID: 3114 SIG: 9
Disconnected from the target VM, address: 'localhost:8628', transport: 'socket'
I really would appreciate some help with this. Thank you in advance!