Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationDrawerFragment not working with AppCompat Toolbar

I'm using the NavigationDrawer created by the template (when starting a new project). I'm trying to get Material Design compatibility so I'm using AppCompat v7. I followed these instructions to set a Toolbar as my ActionBar (i.e. using setSupportActionBar on my toolbar) and I get a NPE in my NavigationDrawerFragment at (inside onCreateView)

 mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1, ...

now I suspect the issues is with the getActionBar() method inside fragment:

   private ActionBar getActionBar() {
    return ((ActionBarActivity) getActivity()).getSupportActionBar();
}

but I have no idea why - I called setSupportActionBar(toolbar) in Activity's onCreate before calling setUp() on the NavigationDrawerFragment...

If anyone has a clue why this is happening please help!

like image 717
Djordje Avatar asked Oct 26 '14 23:10

Djordje


1 Answers

It seems like the problem was that fragment's onCreateView was called before activity's onCreate and thus there was no ActionBar set. I just moved the problematic code into fragment's onActivityCreated and it works like a charm.

like image 93
Djordje Avatar answered Sep 22 '22 10:09

Djordje