so I have a method that initializes my drawer menu and it takes a Drawer
parameter which I use to initialize the drawer. Now I tried to call this drawer parameter to close it after clicking a drawer item, but it gives me this:
Variable 'drawer' is accessed from within inner class, needs to be declare final
so I tried manually to assign final
next to the parameter and it gives me this error on the line where I initialize a new drawer:
Cannot assign a value to final variable 'drawer'
so instead, I let the the IDE to auto fix the first issue and it suggested to copy 'drawer' parameter to a final temp variable. and once I did, no error showed, but when I ran the app it crashed and gives me NPE on the line where I called drawer.close
!!
So how to solve this now..!
The Code:
public void initDrawerMenu(Drawer drawer) {
final Drawer finalDrawer = drawer; // the auto fix
drawer = new DrawerBuilder()
.withActivity(this)
.withMultiSelect(false)
.withOnDrawerItemClickListener(new OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch (position) {
case 0:
finalDrawer.closeDrawer(); // the finalDrawer = null hence causes NPE
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
break;
}
return true;
}
})
.build();
}