为Android应用设置ActionBarSherlock主题

Ara*_*yan 42 android actionbarsherlock

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

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

它现在只是一个骨架活动,它在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
Run Code Online (Sandbox Code Playgroud)

它抱怨的线(PrayerBook:44)是对此的呼唤setContentView.该应用程序只包含一个活动,其中包含一个onCreate()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);
}
Run Code Online (Sandbox Code Playgroud)

我必须错误地设置主题,但我只是不知道如何.有人可以帮忙吗?

更新 下面,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>
Run Code Online (Sandbox Code Playgroud)

但是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').
Run Code Online (Sandbox Code Playgroud)

更新2 在CommonsWare的后续评论帮助中,我能够让它正常运行.我需要添加ActionBarSherlock作为项目依赖项.为此,

1)我删除library-4.0.0.jar,并android-support-4.0.jar从我的项目的lib目录.

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

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

Com*_*are 75

通常,您在清单中设置主题,如Android开发人员文档中所示(并链接到ActionBarSherlock主题页面).

如果您想在应用程序中的任何位置使用ActionBarSherlock,则可以:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock">
Run Code Online (Sandbox Code Playgroud)

  • 确实.我在发布之前尝试了这一点,但我得到/AndroidManifest.xml:7:错误:错误:找不到与给定名称匹配的资源(在'theme'处,值为'@ style/Theme.Sherlock').我会更新问题来指出这一点. (3认同)
  • @ArashPayan:删除您创建的JAR.将ActionBarSherlock添加到您的应用程序作为Android库项目.虽然[ActionBarSherlock文档](http://actionbarsherlock.com/usage.html)(请参阅"包含在您的项目中")仅引用Eclipse和Maven如何执行此操作,但您应该能够将其添加为库项目在NetBeans中按照Android开发人员文档:http://developer.android.com/guide/developing/projects/projects-cmdline.html#ReferencingLibraryProject (2认同)

Tim*_*mmm 19

对我来说,这是因为没有使用@style/前缀.即我有

<style name="AppTheme" parent="Theme.Sherlock.Light" />
Run Code Online (Sandbox Code Playgroud)

代替

<style name="AppTheme" parent="@style/Theme.Sherlock.Light" />
Run Code Online (Sandbox Code Playgroud)

这有点奇怪,因为我发誓默认模板值是这样的:

<style name="AppTheme" parent="android:Theme.Holo.Light" />
Run Code Online (Sandbox Code Playgroud)