Configure an ActionBarSherlock theme for an Android application

READ UPDATE 2 BELOW FOR ANSWER

I am trying to use ActionBarSherlock in my application. I checked the 4.0.0 release from the github repo project , built it in Netbeans, and then copied the library-4.0.0.jar file to my lib project directory (I don't use Eclipse).

This is just skeletal activity right now, and it runs very well in ICS, but when I run it on Gingerbread, I get the following exception, complaining that I do not have the Theme.Sherlock application theme (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 he's complaining about (PrayerBook: 44) is a call to setContentView . An application consists of just one action with the onCreate() method, which I call setTheme() from above:

 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 have to set the theme incorrectly, but I just don’t see how to do it. Can anyone help?

UPDATE Below, CommonsWare noted that the theme can be installed in AndroidManifest.xml. I tried like this:

 <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 throws an error when trying to create an application:

 /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 in his subsequent comments, I was able to get it working. I needed to add ActionBarSherlock depending on the project. For this

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

2) Then go to the library folder inside the root of the ActionBarSherlock directory extracted from github. Enter android update project , so the build.xml and proguard.cfg file will be created for the library.

3) Finally, cd go back to the main directory of the project and add ABS as a library dependency using android update project --path . --library ../ActionBarSherlock/library android update project --path . --library ../ActionBarSherlock/library The path to --library in the command will differ depending on where you checked the repo. ActionBarSherlock and my application project directory were sisters directories.

+42
android actionbarsherlock
Mar 18 2018-12-18T00:
source share
7 answers

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

If you want to use ActionBarSherlock everywhere in your application, this works:

 <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Sherlock"> 
+75
Mar 18 '12 at 11:10
source share

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

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

instead

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

Which type is weird because I swear the default value of the template looks something like this:

 <style name="AppTheme" parent="android:Theme.Holo.Light" /> 
+19
Nov 01
source share
 <!--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 the "apklib" type, meaning that maven will use all sources (resources too). Another jar file dependency (“provided” area) is used to achieve a reference to sherlock classes during encoding.

+3
Jul 30 '12 at 16:50
source share

If you want to use your own style, I created a template for the ActionBarSherlock custom style . The theme is defined in the /values/styles.xml file. This is extended from Theme.Sherlock.Light theme. There are many options you can set: icon, logo, separator, name, shadow overlay, action menu buttons, pop-up menu, action mode background, navigation drop-down list, tab style, display options, etc. Almost everything you need to create your own action bar theme. I use this template in my applications because it helps me quickly style the action bar.

This template can be found on GitHub . It is very easy to use. Just copy the values ​​and select directiories in your project and set the android: theme parameter in the application element in AndroidManifest.xml :

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

Drawables in my template were generated using the Android Action Style Generator . I use different naming conventions for resources. This simple script renames all resources created using the Android Action Bar styles generator to my own convention used in the template.

+1
Jan 16 '13 at 19:31
source share

There was the same problem. The application crashed until the theme was installed in the manifest. Setting the theme programmatically resolved the problem:

 @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.AppTheme); super.onCreate(savedInstanceState); 
+1
Apr 17 '13 at 12:12
source share

This happened to me at IntelliJ IDEA. I solved this problem by choosing File> Project Structure> Project Settings> Facets> Select Action Block in the 2nd column> Select the Library Module> Apply and OK> Save and Rebuild.

UPDATE . After further consideration, my initial answer may be too specific and will probably only work in certain cases. This error means that the dependencies were not configured correctly. In IntelliJ IDEA, follow these steps to properly configure actionbarsherlock as a module dependency:

  • Download and extract the actionbarsherlock library from zip (Note. You only need the "actionbarsherlock" folder).
  • Copy and paste it with your Android project
  • Open a project in IntelliJ
  • File> Project structure> Project settings> Modules> Add (green + above the second column)> Import module> Select the action directory in the file browser> OK> Create a module from existing sources> Next
  • Uncheck "test" so that it is not added to the project and click "Next"
  • On the "Libraries" tab, you need to check the support for android-support-v4, click "Next"
  • On the "Modules" tab, check the actionbarsherlock checkbox, click "Next"
  • If he asks you to overwrite actionbarsherlock.iml, select "Overwrite" and then "Finish" (you should be returned to the "Modules" section of the "Project Structure" dialog box).
  • Select the actionbarsherlock module from the second column and on the "Dependencies" tab in the third column, select the "Export" checkbox next to the android-support-v4 library.
  • Almost there!
  • Now select the project module from the second column, and on the Dependencies tab, click the Add button (green + in the right part of the dialog box).
  • Select Dependency Module and select actionbarsherlock in the dialog that appears and click "OK"
  • Now click "Apply" or "OK" to accept the changes.

That should do the trick.

+1
Jul 24 '13 at 12:16
source share

This is not an answer to OP's question, but I will simply describe how I managed to get the same exception that he mentions, in the hope that it can 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 too long to find it. Everything you read about this exception says “check that you have indicated the subject in the manifest.xml file”, so I quickly looked through the manifest.xml file and here it is. So, I tried different things.

Finally, I looked at the manifest.xml file in detail. I made a mistake by indicating the topic for the main activity, and not for the entire application!

0
Sep 01 '13 at 23:07 on
source share



All Articles