Android get Support Action Bar始终返回NULL [英] Android get Support Action Bar always returns NULL

查看:57
本文介绍了Android get Support Action Bar始终返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在某个项目上使用支持库v7.我正在尝试实例化支持操作栏,如下所示:

I'm using the support library v7 on a certain project. I'm trying to instantiate the support action bar like this:

ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.top_action_bar);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setHomeButtonEnabled(true);

一切都在API lvl低于11的Android设备上正常运行.但是,当尝试在更新的设备上运行相同的代码时,例如具有Android 4+的Samsung Galaxy S4,getSupportActionBar()始终返回null,从而导致预期的崩溃. 我正在使用此样式文件:

Everything works fine on Android devices with API lvl below 11. However, while trying to run the same code on newer devices, like a Samsung Galaxy S4 with Android 4+, getSupportActionBar() always returns null, causing the expected crash. I'm using this style file:

<style name="my_style" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/my_style_ab</item>
    <item name="android:windowNoTitle">true</item>

</style>

<style name="my_style_ab" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:height">70dp</item>
</style>

任何提示可能有什么问题吗?我认为这与样式有关,或者也许我只是无法在11以上的API lvl上使用支持的ActionBar.

Any hints on what may be wrong? I think it has something to do with the style, or maybe I just can't use the support ActionBar on API lvl above 11. Thanks in advance.

推荐答案

android:windowNoTitletrue一起使用会隐藏您的ActionBar

Using android:windowNoTitle with true is hiding your ActionBar

这篇关于Android get Support Action Bar始终返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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