[英]NavigationDrawerFragment not working with AppCompat Toolbar
I'm using the NavigationDrawer created by the template (when starting a new project). 我正在使用模板创建的NavigationDrawer(在启动新项目时)。 I'm trying to get Material Design compatibility so I'm using AppCompat v7.
我正在尝试获得Material Design兼容性,因此我正在使用AppCompat v7。 I followed these instructions to set a Toolbar as my ActionBar (ie using setSupportActionBar on my toolbar) and I get a NPE in my NavigationDrawerFragment at (inside onCreateView)
我按照这些说明将工具栏设置为我的ActionBar(即在我的工具栏上使用setSupportActionBar),然后在我的NavigationDrawerFragment中获取一个NPE(在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: 现在我怀疑问题与片段内的getActionBar()方法有关:
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... 但是我不明白为什么 - 在调用NavigationDrawerFragment上的setUp()之前,我在Activity的onCreate中调用了setSupportActionBar(工具栏)...
If anyone has a clue why this is happening please help! 如果有人知道为什么会这样,请帮忙!
It seems like the problem was that fragment's onCreateView was called before activity's onCreate and thus there was no ActionBar set. 似乎问题是片段的onCreateView在activity的onCreate之前被调用,因此没有设置ActionBar。 I just moved the problematic code into fragment's onActivityCreated and it works like a charm.
我只是将有问题的代码移动到片段的onActivityCreated中,它就像一个魅力。
用getActivity()
替换getActionBar().getThemedContext()
getActivity()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.