问题
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.
回答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">
回答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" />
回答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.
回答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);
回答5:
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:
- Download and extract the actionbarsherlock library from the zip (Note: You only need the “actionbarsherlock” folder)
- Copy and paste it alongside your Android project
- Open your project in IntelliJ
- 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
- Uncheck the “test” folder so it is not added to the project and click “Next”
- Under the Libraries tab, android-support-v4 should be checked, click “Next”
- Under the Modules tab, actionbarsherlock should be checked, click “Next”
- 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)
- 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
- Almost there!
- 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)
- Select Module Dependency and choose actionbarsherlock from the dialog that pops up and press “OK”
- Now click “Apply” or “OK” to accept the changes
That should do the trick.
回答6:
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.
回答7:
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!
来源:https://stackoverflow.com/questions/9757400/setting-actionbarsherlock-theme-for-android-app