Djordje
Djordje

Reputation: 186

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!

Upvotes: 6

Views: 2141

Answers (2)

Djordje
Djordje

Reputation: 186

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.

Upvotes: 6

SooCheng Koh
SooCheng Koh

Reputation: 2312

just replace getActionBar().getThemedContext() with getActivity()

Upvotes: 4

Related Questions