,讀取,更新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