-1

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();
  }
Alaa AbuZarifa
  • 1,171
  • 20
  • 39
  • I would add a quick if statement at the beginning of `initDrawerMenu` and check if `drawer` is null, and log and exit if it is null. My guess is that `drawer` is null from the beginning. – emerssso Apr 02 '18 at 21:20
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Brian Apr 02 '18 at 21:33

2 Answers2

1

That's probably because your drawer in the initDrawerMenu is null or getting nulled outside your method.

If you want to initialise Drawer, you need to change the method to return the Drawer instead passing your Drawer variable to your method. Hence, the initDrawerMenu don't depends on drawer variable.

From your code, I think the thing that you want to do is creating a drawer not initialise it from existing drawer. So, you're better to change the method name and use a return for creating your Drawer. Something like this:

public Drawer initDrawerMenu() {
  final Drawer drawer = new DrawerBuilder()
        .withActivity(this)
        .withMultiSelect(false)
        .withOnDrawerItemClickListener(new OnDrawerItemClickListener() {
          @Override
          public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            switch (position) {
              case 0:
                drawer.closeDrawer(); 
                startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                break;
            }
            return true;
          }
        })
        .build();

  return drawer;
}

You should change the method name to more readable, with something like:

public Drawer createDrawerMenu() {
  ...
}

When you want to use it, you just call it with this:

Drawer drawer = createDrawerMenu();
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • It gives me an error on this line `drawer.closeDrawer();` that `Variable 'drawer' might not have been initialized` ? – Alaa AbuZarifa Apr 03 '18 at 19:14
  • move the `new OnDrawerItemClickListener()` to outside the method so it becomes class variable. – ישו אוהב אותך Apr 03 '18 at 20:43
  • Since this method is inside base activity and all the activities in my app extend it, I changed the `drawer` to a class variable inside the base activity and the error is gone and it's working fine. Thanks. – Alaa AbuZarifa Apr 04 '18 at 07:07
0

I have used DrawerLayout and NavigationView. Hope this helps

    public class MyActivity extends Activity implements OnNavigationItemSelectedListener{

     private DrawerLayout drawer;

     @Override
        public void onCreate(Bundle savedInstanceState) { 
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
     }

    @Override
        public boolean onNavigationItemSelected(MenuItem item) {

             //close the drawer
             drawer.closeDrawers();
             switch (item.getItemId()) {
                case R.id.item:
                //do something
                break;
             }
    }
}

P.S -> here R.id.item is a MenuItem

you can set the menu to the NavigationView like this

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/my_navigation_items" />

 </android.support.v4.widget.DrawerLayout>
Deepak kaku
  • 1,218
  • 9
  • 15