添加一个操作栏,以Theme.Black.NoTitleBar的Andr​​oid [英] Adding an action bar to Theme.Black.NoTitleBar Android

查看:156
本文介绍了添加一个操作栏,以Theme.Black.NoTitleBar的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式添加操作栏如图所示 dev的文件建立但我来了跨越一个错误。我minSdk设置为11,我的应用程序有一个布局和一个活动的唯一code。在活动:

I'm trying to add the action bar programatically as shown in the dev documentaion but I'm coming across an error. My minSdk is set to 11, my application has one layout and one activity and the only code in the activity is:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inbox);
    ActionBar actionBar = getActionBar();
    actionBar.show();

}

如果我拿出最后两行,然后我的应用程序运行。我知道了全息主题自动包含动作条,但我不喜欢 EDITTEXT 的意见。为什么任何想法正在发生的事情。我应该使用 HoloTheme 和不同的更换主题皮肤的看法?

If I take out those last two lines then my app runs. I know that the holo theme include the actionbar automatically, but I don't like the editTextviews. Any ideas on why this is happening. Should I be using the HoloTheme and themeing the views differently?

此外,我没有得到在Eclipse中的任何错误。我的计划是从我从logcat的破译为空指针异常崩溃。

Again, I'm not getting any errors in eclipse. My program is crashing from what I can decipher from logcat as an null pointer exception.

推荐答案

从文档您链接到:

如果你有一个自定义的活动主题,你会在其中要删除的操作栏,设置安卓windowActionBar 样式属性。但是,如果你使用一个主题删除操作栏,则窗口将不允许操作栏可言,所以你不能添加它后面叫 getActionBar()返回

If you have a custom activity theme in which you'd like to remove the action bar, set the android:windowActionBar style property to false. However, if you remove the action bar using a theme, then the window will not allow the action bar at all, so you cannot add it later—calling getActionBar() will return null.

由于您使用的主题没有行动起来吧, getACtionBar()将返回,然后你试图调用显示(),导致异常被抛出。

Since you're using a theme without an action bar, getACtionBar() is returning null, and then you're attempting to call show() on that null, resulting in an Exception being thrown.

这样解释你得到的错误。至于该怎么办呢,文档动作条说:

So that explains the error you're getting. As far as what to do about it, the documentation for ActionBar says:

与Android 3.0(API级别11)开始,操作栏出现在活动窗口的顶部活动时将使用系统的全息主题(或其后代主题之一),这是默认的。您可以通过调用 requestFeature(FEATURE_ACTION_BAR)或宣布它在一个自定义主题与否则添加操作栏上的 windowActionBar 属性。

Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default. You may otherwise add the action bar by calling requestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with the windowActionBar property.

这是给你两个选择容易有一个动作条不使用的Holo主题。这可能是最简单的,你的情况:

That gives you two easy options to have an ActionBar without using a Holo theme. This is probably simplest in your case:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); // Add this line
    setContentView(R.layout.inbox);
    ActionBar actionBar = getActionBar();
    actionBar.show();
}

更新:您应该也切换到刚 Theme.Black 而不 NoTitleBar 部分,因为这$ P $从工作pvents的动作条。

Update: You should also switch to just Theme.Black without the NoTitleBar part, as that prevents the ActionBar from working.

这篇关于添加一个操作栏,以Theme.Black.NoTitleBar的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆