Hamburger Icon with MaterialDrawer

300 Views Asked by At

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

There are 1 best solutions below

0
On

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.