android - 为 Android 应用设置 ActionBarSherlock 主题

标签 android actionbarsherlock

阅读下面的更新 2 以获得答案

我正在尝试在我的应用中使用 ActionBarSherlock。我从 project github repo 查看了 4.0.0 版本,在 Netbeans 中构建它,然后将 library-4.0.0.jar 文件复制到我项目的 lib 目录中(我没有使用 Eclipse)。

它现在只是一个骨架 Activity ,它在 ICS 中启动得很好,但是当我在 Gingerbread 上运行它时,我收到以下异常,提示我没有 Theme.Sherlock(或类似)的应用程序主题:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arashpayan.prayerbook/com.arashpayan.prayerbook.PrayerBook}: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:987)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:899)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:852)
    at com.actionbarsherlock.ActionBarSherlock.setContentView(ActionBarSherlock.java:655)
    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:316)
    at com.arashpayan.prayerbook.PrayerBook.onCreate(PrayerBook.java:44)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    ... 11 more

它提示的那一行 (PrayerBook:44) 是对 setContentView 的调用。该应用程序仅包含一个带有 onCreate() 方法的 Activity ,我从顶部调用该方法 setTheme():

public void onCreate(Bundle savedInstanceState)
{
        setTheme(com.actionbarsherlock.R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);

        TextView rootTextView = new TextView(this);
        rootTextView.setText("Hello, world!");
        setContentView(rootTextView);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab tab = getSupportActionBar().newTab();
        tab.setText("Prayers");
        getSupportActionBar().addTab(tab);

        tab = getSupportActionBar().newTab();
        tab.setText("Recents");
        getSupportActionBar().addTab(tab);

        tab = getSupportActionBar().newTab();
        tab.setText("Bookmarks");
        getSupportActionBar().addTab(tab);
}

我一定是错误地设置了主题,但我就是不明白怎么做。有人可以帮忙吗?

更新 下面,CommonsWare 注意到可以在 AndroidManifest.xml 中设置主题。我试过这样:

<application android:label="@string/app_name" android:icon="@drawable/icon" android:theme="@style/Theme.Sherlock">
        <activity android:name="PrayerBook"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden|screenLayout|uiMode|mcc|mnc|locale|navigation|fontScale|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="LanguagesActivity" />
</application>

但是 Ant 在尝试构建应用程序时给了我一个错误:

/Users/arash/coding/prayerbook/AndroidManifest.xml:7: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Sherlock').

更新 2 在 CommonsWare 在他的后续评论中的帮助下,我能够让它工作。我需要将 ActionBarSherlock 添加为项目依赖项。为此,

1) 我从项目的 lib 目录中删除了 library-4.0.0.jarandroid-support-4.0.jar

2) 接下来,导航到从 github checkout 的 ActionBarSherlock 目录根目录内的 library 文件夹。键入 android update project 以便为库创建 build.xmlproguard.cfg 文件。

3) 最后,cd 回到主项目目录并使用 android update project --path 添加 ABS 作为库依赖项。 --library ../ActionBarSherlock/library 命令中 --library 的路径将根据您 checkout repo 的位置而有所不同。 ActionBarSherlock 和我的应用程序的项目目录是同级目录。

最佳答案

通常,您在 list 中设置主题,如 the Android developer documentation 所示(并链接到 the ActionBarSherlock theming page )。

如果您想在应用程序中的任何地方使用 ActionBarSherlock,这可行:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock">

关于android - 为 Android 应用设置 ActionBarSherlock 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757400/

相关文章:

java - 使用 DialogTitleButton 切换大小写

android - getExternalCacheDir() 如何在 Android 上运行?

android - AdMob 横幅的关闭按钮

android - 为什么我的布局 OnItemClickListener 现在不工作?

android - 使用 ActionBarSherlock/HoloEverywhere Light 的对话框

java - 从将在 Android 应用程序中具有全局范围的代码中设置区域设置

android - 添加 ActionBarSherlock 库会产生涉及 android.support.v4 的错误

android - 转换为 Dalvik 格式失败,出现错误 1 ​​adt r18

android - 文本在 Sherlock 的操作栏选项卡中全部大写

android - Actionbarsherlock 开关操作栏覆盖模式以编程方式