NavigationDrawerFragment不工作AppCompat工具栏 [英] NavigationDrawerFragment not working with AppCompat Toolbar
问题描述
我使用的模板(开始一个新项目时)创建的NavigationDrawer。 我试图让材料设计的兼容性,所以我用AppCompat V7。 我跟着<一个href="http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html">these说明设置工具栏作为我的动作条(即使用我的工具栏上setSupportActionBar),我得到我的NavigationDrawerFragment一个NPE在(内部onCreateView)
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, ...
现在我怀疑的问题是与getActionBar()片段里面的方法:
now I suspect the issues is with the getActionBar() method inside fragment:
private ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
但我不知道为什么 - 我叫setSupportActionBar(工具栏)在活动的onCreate调用设置()在NavigationDrawerFragment ...
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!
推荐答案
这似乎是问题是片段的onCreateView之前活动的onCreate叫,因此没有动作条集。我刚刚搬进了有问题的code为碎片的onActivityCreated,它就像一个魅力。
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.
这篇关于NavigationDrawerFragment不工作AppCompat工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!