2014-02-25 43 views
0

我使用自定義的主題,下面的代碼: -自定義主題,則必須使用Theme.Sherlock,Theme.Sherlock.Light,Theme.Sherlock.Light.DarkActionBar,或衍生

custom_styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CustomWindowTitleBackground"> 
     <item name="android:background">#FFFFFF</item> 

</style> 

<style name="CustomTheme" parent="android:Theme"> 
    <item name="android:windowTitleSize">40dp</item> 
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    <item name="android:windowBackground">@color/white</item> 


</style> 

的Manifest.xml

<application 
    android:name="com.exmaple.test.MainApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme" > 
    <activity 
     android:name="com.exmaple.test.FirstActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
</application> 

Menucentre.java

ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayUseLogoEnabled(false); 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(false); 
    @SuppressWarnings("deprecation") 
    LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    View cView = getLayoutInflater().inflate(R.layout.nav_bar_smart, null); 
    cView.setBackgroundColor(Color.parseColor(HeaderBackground)); 
    actionBar.setCustomView(cView, layout); 

輸出與此錯誤:

02-25 10:14:15.645: E/AndroidRuntime(6542): FATAL EXCEPTION: main 
02-25 10:14:15.645: E/AndroidRuntime(6542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MenuCenter}: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative. 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread.access$500(ActivityThread.java:122) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.os.Looper.loop(Looper.java:132) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread.main(ActivityThread.java:4123) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at java.lang.reflect.Method.invoke(Method.java:491) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at dalvik.system.NativeStart.main(Native Method) 
02-25 10:14:15.645: E/AndroidRuntime(6542): Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative. 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:976) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:902) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:855) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:267) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.slidingmenu.lib.app.SlidingFragmentActivity.setContentView(SlidingFragmentActivity.java:75) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.slidingmenu.lib.app.SlidingFragmentActivity.setContentView(SlidingFragmentActivity.java:67) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.slidingmenu.lib.app.SlidingFragmentActivity.setContentView(SlidingFragmentActivity.java:59) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at com.example.test.MenuCenter.onCreate(MenuCenter.java:70) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
02-25 10:14:15.645: E/AndroidRuntime(6542):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 

回答

1

更換

<style name="CustomTheme" parent="android:Theme"> 

<style name="CustomTheme" parent="Theme.Sherlock"> 
+0

代替我這個代碼this.requestWindowFeature後(Window.FEATURE_CUSTOM_TITLE); \t this.getWindow()。setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.nav_bar);出現錯誤您無法將自定義標題與其他標題功能結合起來 – micky

+0

您必須根據[此問題]根據ActionBarSherlock在'super.onCreate()'之前調用'requestWindowFeature'(https://github.com/JakeWharton/ActionBarSherlock/issues/310#issuecomment-7350074) – ianhanniballake