【发布时间】:2013-08-30 06:11:54
【问题描述】:
所以我在使用 ActionBarSherlock 并决定切换到新的 ActionBarCompat。使用 ABS,可以使用本文中描述的方式隐藏 ActionBar: How to hide action bar before activity is created, and then show it again?
但是,使用 ActionBarCompat,应用程序在 API14 上崩溃,因为当您将 android:windowActionBar 设置为 false 时,getSupportActionBar() 方法返回 null,即使您已将 getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 声明到 onCreate() 方法中。
有趣的是,如果你改为调用getActionBar(),你会得到对象并且一切正常。
那么,这是一个错误还是我错过了什么?欢迎任何想法!
styles.xml文件:
<style name="Theme.MyApp" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>
</style>
MyActivity.java文件:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the action bar feature. This feature is disabled by default into the theme
// for specific reasons.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
...
// By default the action bar is hidden.
getSupportActionBar().hide();
}
【问题讨论】:
-
@Johnson accordingly to the docs,不。 “ActionBarCompat 包含它自己的 ActionBar 类,并且要检索附加到您的活动的 Action Bar,您调用 getSupportActionBar()。”
-
你解决过这个问题吗?我遇到了同样的问题。
-
@Studio4Development 不,最好的选择是创建一个类来处理不同类型的 ActionBar(本机和支持库)并调用正确的类。这是一个想法,还没有使用它,仍然使用 ABS 而不是 ABC。 ://
标签: android android-actionbar android-actionbar-compat