1

I'm trying to add to my toolbar the hamburger icon using the MaterialDrawer library. And I could put the icon the icon into the toolbar with this code

        // Handle Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);   
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_name);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Before that I create the drawer

Drawer result = new DrawerBuilder()
 ...
 .build();

Finally, I use the instructions of the library to add the hamburger icon

result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);

And when I execute the code, it give me the follow error in the las line and dont run the application

Unable to start activity ComponentInfo{com.example.sergi.drawerexample/com.example.sergi.drawerexample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.setDrawerIndicatorEnabled(boolean)' on a null object reference

Someone could help me, I dont know what its happend

Thanks

  • 1
    typical nullpointer exception. Check the stacktrace to see which object is null and make sure you initialize it first. – Zun May 18 '18 at 09:07
  • 1
    also read this https://github.com/mikepenz/MaterialDrawer/issues/650 – Zun May 18 '18 at 09:08

1 Answers1

0

As zunjae correctly has pointed out you must check why that object is null and try to initialize it before calling their methods. zunjae have sent you to an issue where you can find the solution:

result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(toolbar)
            .build();

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);

I hope this helps you.

Álvaro Loza
  • 411
  • 4
  • 21