Setting ActionBarSherlock Theme for Android app

READ UPDATE 2 BELOW FOR THE ANSWER

I'm trying to use ActionBarSherlock in my app. I checked out the 4.0.0 release from the project github repo, built it in Netbeans, then copied the library-4.0.0.jar file into my project's lib directory (I'm not using Eclipse).

It's just a skeleton activity right now, and it launches just fine in ICS, but when I run it on Gingerbread I get the following exception complaining that I haven't the app theme to Theme.Sherlock (or similar):

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

The line it complains about (PrayerBook:44) is the call to setContentView. The app just consists of a single activity with an onCreate() method that I call setTheme() from at the top:

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);
}

I must be setting the theme incorrectly, but I just don't see how. Can anyone help?

UPDATE Below, CommonsWare noted that the theme can be set in the AndroidManifest.xml. I've tried that like so:

<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>

but Ant gives me an error when it tries to build the app:

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

UPDATE 2 With CommonsWare's help in his follow up comments, I was able to get it working. I needed to add ActionBarSherlock as a project dependency. To do so,

1) I removed library-4.0.0.jar and android-support-4.0.jar from my project's lib directory.

2) Next, navigate into the library folder inside the root of the ActionBarSherlock directory checked out from github. Type android update project so a build.xml and proguard.cfg file will be created for the library.

3) Finally, cd back into the main project directory and add ABS as a library dependency with android update project --path . --library ../ActionBarSherlock/library The path to the --library in the command will vary according to where you checked out the repo. ActionBarSherlock and my app's project directory were sibling directories.


Solution 1:

Usually, you set your theme in the manifest, as shown in the Android developer documentation (and linked to from the ActionBarSherlock theming page).

If you want to use ActionBarSherlock everywhere within your app, this works:

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

Solution 2:

For me this was caused by not using the @style/ prefix. I.e. I had

<style name="AppTheme" parent="Theme.Sherlock.Light" />

instead of

<style name="AppTheme" parent="@style/Theme.Sherlock.Light" />

Which is kind of weird because I swear the default template value is something like:

<style name="AppTheme" parent="android:Theme.Holo.Light" />

Solution 3:

<!--Provides resources-->
<dependency>
   <groupId>com.actionbarsherlock</groupId>
   <artifactId>library</artifactId>
   <version>4.1.0</version>
   <type>apklib</type>
</dependency>
<!-Provides import links-->
<dependency>
   <groupId>com.actionbarsherlock</groupId>
   <artifactId>library</artifactId>
   <version>4.1.0</version>
   <type>jar</type>
   <scope>provided</scope>
 </dependency>

The hint is to use type "apklib", that's mean that maven will be using all sources (resources too). Other dependecy to jar file (scope "provided") is used to achieve link to sherlock classes during coding.

Solution 4:

Had the same problem. The app crashed altough the theme was set in the manifest. Setting the theme programmatically solved the problem:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);