使用导航抽屉时调用getActionBar()。setDisplayShowTitleEnabled(false)

时间:2014-08-25 19:49:25

标签: java android android-fragments android-actionbar

我有一个活动,主持一个包含2个片段的ViewPager。我想删除操作栏中的内容,以便ViewPager的选项卡在操作栏中显示,所以我使用它:

getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);

问题是,当我尝试在此活动上添加导航抽屉时,我收到此错误:

08-25 15:37:08.903: E/AndroidRuntime(18059): FATAL EXCEPTION: main
08-25 15:37:08.903: E/AndroidRuntime(18059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.TestMap}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.os.Looper.loop(Looper.java:137)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread.main(ActivityThread.java:5289)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at java.lang.reflect.Method.invokeNative(Native Method)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at java.lang.reflect.Method.invoke(Method.java:525)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at dalvik.system.NativeStart.main(Native Method)
08-25 15:37:08.903: E/AndroidRuntime(18059): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
08-25 15:37:08.903: E/AndroidRuntime(18059):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:279)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.Activity.requestWindowFeature(Activity.java:3264)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:62)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at com.example.test.BaseActivity.onCreate(BaseActivity.java:38)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at com.example.test.TestMap.onCreate(TestMap.java:29)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.Activity.performCreate(Activity.java:5133)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-25 15:37:08.903: E/AndroidRuntime(18059):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)

这是我的活动:

public class TestMap extends BaseActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
String TabFragmentB;
// Tab titles
private String[] tabs = { "List View", "Map View" };

private String[] navMenuTitles;
private TypedArray navMenuIcons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getActionBar().setDisplayShowHomeEnabled(false);
    getActionBar().setDisplayShowTitleEnabled(false);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_map);

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    set(navMenuTitles, navMenuIcons);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

public void setTabFragmentB(String t) {
    TabFragmentB = t;
}

public String getTabFragmentB() {
    return TabFragmentB;
}

}

所以,基本上,如果我删除extends BaseActivity以及涉及此类的所有代码,它就可以工作,并且导航抽屉打开(它只是不能根据我的需要进行定制)。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你应该在setContentView:

之后调用getActionBar()
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_map);

    getActionBar().setDisplayShowHomeEnabled(false);
    getActionBar().setDisplayShowTitleEnabled(false);
//code ...