getActionBar 返回 null [英] getActionBar returns null
问题描述
调用 getActionBar
返回 null
.这经常被报告,所以我确保包括其他人使用的解决方案:我的 minSdkVersion=11
,我有一个标题栏,我正在调用 getActionBar
在 setContentView
之后.此外,我的活动不是儿童活动.
Calling getActionBar
returns null
. This has been frequently reported so I've made sure to include the solutions others have used: My minSdkVersion=11
, I do have a titlebar, and I'm calling getActionBar
after setContentView
. Also, my activity is not a child activity.
setContentView(R.layout.main);
// experiment with the ActionBar
ActionBar actionBar = getActionBar();
actionBar.hide();
设备是运行 Android 3.2 的三星 Galaxy Tab 10.1
Device is a Samsung Galaxy Tab 10.1 running Android 3.2
预先感谢您的任何想法或建议!
Thanks in advance for any ideas or suggestions!
推荐答案
看来您需要通过主题或以下代码请求拥有操作栏 (!= 标题栏).
It seems you need to request having an Actionbar (!= titlebar) either via a Theme or via below code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The Action Bar is a window feature. The feature must be requested
// before setting a content view. Normally this is set automatically
// by your Activity's theme in your manifest. The provided system
// theme Theme.WithActionBar enables this for you. Use it as you would
// use Theme.NoTitleBar. You can add an Action Bar to your own themes
// by adding the element <item name="android:windowActionBar">true</item>
// to your style definition.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main);
// experiment with the ActionBar
ActionBar actionBar = getActionBar();
actionBar.hide();
}
来自 [此处的代码]
这篇关于getActionBar 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!