,读取,更新2下面的答案设置ActionBarSherlock的主题为Android应用
我想在我的应用程序使用ActionBarSherlock。我检查了project github repo的4.0.0版本,在Netbeans中构建它,然后将library-4.0.0.jar文件复制到我的项目的lib目录中(我没有使用Eclipse)。
这只是一个骨架活动,现在它在ICS中启动得很好,但是当我在姜饼上运行它时,我得到以下异常,抱怨我没有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
它抱怨的线(祈祷书: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);
}
我一定要正确设置主题,但我只是不明白怎么样。谁能帮忙?
UPDATE 下面,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>
但蚂蚁给我一个错误:
/Users/arash/coding/prayerbook/AndroidManifest.xml:7: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Sherlock').
更新2 随着CommonsWare在他跟进的评论帮助,我能够得到它的工作。我需要添加ActionBarSherlock作为项目依赖项。为此,
1)我从我的项目的lib
目录中删除library-4.0.0.jar
和android-support-4.0.jar
。
2)接下来,导航到从github签出的ActionBarSherlock目录的根目录下的library
文件夹。输入android update project
,即会为库创建一个build.xml
和proguard.cfg
文件。
3)最后,cd
回主项目目录,并与android update project --path . --library ../ActionBarSherlock/library
路径的命令添加ABS作为库依赖于--library
将根据您签出回购有所不同。 ActionBarSherlock和我的应用程序的项目目录是兄弟目录。
去ABS github下载示例ABS应用程序的源代码,并看看 – xDragonZ 2012-03-18 10:39:51
这是我检查的第一个地方。在ABS演示应用程序中,SampleList类的静态THEME成员被初始化为R.style.Theme_Sherlock,但从未在该活动中使用,除非从选项菜单中更改其存储值。所有从该主要活动开始的活动都以这个常量作为onCreate()方法最顶层的参数调用setTheme(),这就是为什么我在代码中执行相同操作的原因。您会注意到ABS网站上“家长主题”部分的第二段中也列出了相同的说明。 http://actionbarsherlock.com/theming.html – 2012-03-18 10:49:43