gpt4 book ai didi

java - 使用抽屉导航时调用 getActionBar().setDisplayShowTitleEnabled(false)

转载 作者:行者123 更新时间:2023-11-30 02:42:55 25 4
gpt4 key购买 nike

我有一个 Activity,它承载一个带有 2 个 fragment 的 ViewPager。我想删除 Action Bar 中的内容,以便 ViewPager 的选项卡将出现在 Action Bar 中,所以我使用这个:

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

问题是,当我尝试将抽屉导航添加到此 Activity 时,出现此错误:

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)

这是我的 Activity :

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 和涉及此类的所有代码,它就可以工作,并且抽屉导航会打开(它不是根据我的需要定制的)。有什么想法吗?

最佳答案

您应该在 setContentView 之后调用 getActionBar():

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_map);

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

关于java - 使用抽屉导航时调用 getActionBar().setDisplayShowTitleEnabled(false),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com