java - Android:在 ActionBarSherlock 项目中的 Android 2.3 上,按钮的 findViewById 返回 null

标签 java android actionbarsherlock android-2.3-gingerbread

我正在开发 Android 项目,目标版本是 4.2.2,但最低支持版本是 2.3。该项目大量使用 ActionBarSherlock 库。在 list 文件中我写道:

 android:theme="@style/Theme.Sherlock" >

当我在 Android 4.2.2 设备和 Android 4.0 上测试我的项目时,一切工作正常,但当我尝试在 Android 2.3 上运行该应用程序时,它崩溃并显示以下日志: enter image description here

在调试过程中,我注意到自定义 ActionBar (custom_bar) 上的一些按钮为空。我使用此代码来创建自定义 ActionBar:

 ActionBar theBar = getSupportActionBar();
    theBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    theBar.setCustomView(R.layout.custom_bar);

那么如何解决这个问题?某些 ActionBarSherlock 库功能是否与 2.3 兼容?

最佳答案

来自doc :

The themes should be defined in your manifest for the entire application or on a per-activity basis. You may also define the theme in the code of each activity before calling super.onCreate(Bundle). This must be done for every activity on which you extend from one of the 'Sherlock' activity base classes and intend to use the action bar. More information on how to specify a theme can be found in the official Android documentation.

您是否在 onCreate 方法中指定了这一点?

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_Sherlock);
    super.onCreate(savedInstanceState);
   //other stuff here
}

关于java - Android:在 ActionBarSherlock 项目中的 Android 2.3 上,按钮的 findViewById 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16525596/

相关文章:

java - 带有 Collections$UnmodifiableCollection.add(未知来源)的 Spring RestTemplate UnsupportedOperationException

android - 如何完全隐藏 ExpandableListView 的 groupIndicator?

android - ViewPager 和 ActionBar 的奇怪错误/行为 (Sherlock)

android - 请问我自己项目的APK文件中的ActionbarSherlock?

android - 如何在 Android 操作栏中居中图标

java - 如何在 Java 中将对象转换为查询字符串?

java - JSP中的登录验证和 session

c# - Java 等同于 C# 的委托(delegate)/事件 : Changing Event Invoked Code Dynamically

android - 建议我从 websocket 服务器检索实时数据

android - 如何更改抽屉导航中的标题