42

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.

Arash Payan
  • 1,793
  • 1
  • 17
  • 19
  • Go download the sample ABS app source code at ABS github and take a look – xDragonZ Mar 18 '12 at 10:39
  • That was the first place I checked. In the ABS demos app, the static THEME member of the SampleList class is initialized to R.style.Theme_Sherlock but never used in that activity except for changing it's stored value from the options menu. All activities started from that main activity call setTheme() with that constant as the argument at the very top of their onCreate() method, which is why I do the same in my code. You'll notice the same instruction is also listed in the second paragraph of the 'Parent Themes' section on the ABS site. http://actionbarsherlock.com/theming.html – Arash Payan Mar 18 '12 at 10:49

7 Answers7

75

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">
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    Indeed. I tried that very thing before posting, but I get /AndroidManifest.xml:7: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Sherlock'). I'll update question to point this out. – Arash Payan Mar 18 '12 at 11:23
  • 2
    @ArashPayan: Delete the JAR that you created. Add ActionBarSherlock to your app as an Android library project. While [the ActionBarSherlock documentation](http://actionbarsherlock.com/usage.html) (see "Including In Your Project") only cites Eclipse and Maven for how to do this, you should be able to add it as a library project in NetBeans by following the Android developer documentation: http://developer.android.com/guide/developing/projects/projects-cmdline.html#ReferencingLibraryProject – CommonsWare Mar 18 '12 at 11:28
  • That did the trick. I had to remove the android-support-v4.jar file as well, because that's included as part of ABS. I'll outline all the steps I took above. Thank you! – Arash Payan Mar 18 '12 at 11:53
19

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" />
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • Just ran into this, thanks for your answer. Definitely wasted too much time trying other things. – Mike P. Mar 30 '13 at 01:23
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.

Community
  • 1
  • 1
1

If you wanna use custom style, I made a template for custom ActionBarSherlock style. Theme is defined in /values/styles.xml file. It is extended from Theme.Sherlock.Light theme. There are many parameters, you can set: icon, logo, divider, title, shadow overlay, action menu buttons, popup menu, action mode background, dropdown list navigation, tab style, display options etc. Almost everything, what you need, to create your custom action bar theme. I use this template in my apps, because it helps me to style action bar very quickly.

You can find this template on my GitHub. It is very easy to use. Just copy values and drawable directiories into your project and set android:theme parameter in application element in AndroidManifest.xml:

<application
    ...
    android:theme="@style/Theme.Example">

Drawables in my template were generated via Android Action Bar Style Generator. I use different naming convention for resources. This simple script renames all resources generated with Android Action Bar Style Generator to my own convention, used in the template.

petrnohejl
  • 7,581
  • 3
  • 51
  • 63
1

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);
Apfelsaft
  • 5,766
  • 4
  • 28
  • 37
  • For what it's worth, that answer is also recommended on this thread http://stackoverflow.com/questions/12864298/java-lang-runtimeexception-theme-sherlock, but Jake Wharton, the author of ActionBarSherlock, wrote in a comment that the answer was wrong. – RenniePet Sep 01 '13 at 22:56
1

This was happening to me in IntelliJ IDEA. I solved this issue by going to File > Project Structure > Project Settings > Facets > Select actionbarsherlock module in 2nd column > Check the "Library module" checkbox > Apply and OK > Save and Rebuild.

UPDATE After further review, my original response may be too specific, and will probably only work in certain cases. This error means that the dependencies have not been properly setup. In IntelliJ IDEA, follow these steps to properly setup actionbarsherlock as a module dependency:

  1. Download and extract the actionbarsherlock library from the zip (Note: You only need the “actionbarsherlock” folder)
  2. Copy and paste it alongside your Android project
  3. Open your project in IntelliJ
  4. File > Project Structure > Project Settings > Modules > Add (green ‘+’ above second column) > Import Module > Select actionbarsherlock directory in file browser dialog > OK > Create module from existing sources > Next
  5. Uncheck the “test” folder so it is not added to the project and click “Next”
  6. Under the Libraries tab, android-support-v4 should be checked, click “Next”
  7. Under the Modules tab, actionbarsherlock should be checked, click “Next”
  8. If it asks you to Overwrite actionbarsherlock.iml, choose “Overwrite” and then "Finish" (You should be returned to the Modules section of the Project Structure dialog)
  9. Select the actionbarsherlock module from the second colum and under the Dependencies tab in the third column, check “Export” next to the android-support-v4 library
  10. Almost there!
  11. Now select your project module from the second column, and under the Dependencies tab, click the Add button (green ‘+’ on the far right side of the dialog)
  12. Select Module Dependency and choose actionbarsherlock from the dialog that pops up and press “OK”
  13. Now click “Apply” or “OK” to accept the changes

That should do the trick.

kahmali
  • 557
  • 6
  • 10
0

This is not an answer for the OP's question, but I'll just describe how I managed to get the same exception as he mentions, in the hopes it may help someone else:

    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.Merlinia.MMessaging_Test/com.Merlinia.MMessaging_Test.TestObjectsActivity}: 
    java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.

In my case the solution was very simple, although it took me way too long to find it. Everything you read about this exception says "check that you've specified the theme in the manifest.xml file", so I took a quick look at my manifest.xml file, and there it was. So then I tried various other things.

Finally, I took a closer look at my manifest.xml file. I'd made the mistake of specifying the theme for the main activity, not for the whole application!

RenniePet
  • 11,420
  • 7
  • 80
  • 106