NavigationDrawerFragment不工作AppCompat工具栏工具栏、工作、NavigationDrawerFragment、AppCompat

由网友(一见钟情不将就)分享简介:我使用的模板(开始一个新项目时)创建的NavigationDrawer。我试图让材料设计的兼容性,所以我用AppCompat V7。我跟着these说明设置工具栏作为我的动作条(即使用我的工具栏上setSupportActionBar),我得到我的NavigationDrawerFragment一个NPE在(内部o...

我使用的模板(开始一个新项目时)创建的NavigationDrawer。 我试图让材料设计的兼容性,所以我用AppCompat V7。 我跟着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.

阅读全文

相关推荐

最新文章