2013-02-26 40 views
1

我爲我的用戶提供主題明暗選項。如何在主要活動api中設置主題7?

在日誌中的錯誤:

Caused by: java.lang.IllegalStateException : You must use Theme.Sherlock , Theme.Sherlock.Light , Theme.Sherlock.Light.DarkActionBar , or a derivative.

setContentView(R.layout.activity_main);在MainActivity會出現錯誤。

MainActivity

public class MainActivity extends SherlockFragmentActivity {.....public static int globalTheme; 
Context context; 
protected void onCreate(Bundle savedInstanceState) { 


    context = getApplicationContext(); 
    mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); 

    Editor editor = mySharedPreferences.edit(); 
    editor.putBoolean("proxy", false); 
    editor.commit(); 

    if (mySharedPreferences.getString(Preferences.PREF_THEME, "1").trim().equals("1")) 
     globalTheme = R.style.Sherlock___Theme; 
    else 
     globalTheme = R.style.Sherlock___Theme_Light; 
    setTheme(globalTheme); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

我沒有在清單中添加的東西,因爲主題應該是動態變化 AndroidManifest.xml中

<uses-sdk android:minSdkVersion="7" 
    /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:name="com.belasheuski.activities.MyApplication" 
    > 
    <activity 
     android:name="com.belasheuski.activities.MainActivity" 
     android:label="@string/name_main" 
     android:screenOrientation="portrait" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

在API 7時出現此錯誤在API的所有15工作得很好。

回答

1

我以這種方式設置此...

setTheme(isLightTheme ? R.style.MyApp_Light : R.style.MyApp); 

在樣式定義正在尋找像...

<style name="MyApp.Light" parent="@style/Theme.Sherlock.Light.ForceOverflow"> 
    <item name="overlayedActionBarBackground">@color/overlayedActionBarLight</item> 

    ... 
</style> 

<style name="MyApp" parent="@style/Theme.Sherlock.ForceOverflow"> 
    <item name="overlayedActionBarBackground">@color/overlayedActionBarDark</item> 

    ... 
</style> 

這工作得很好。所以我認爲你從ABS採用了錯誤的樣式定義。

乾杯!

+0

很好的答案!非常感謝:)我寫了 '我不明白什麼是 」ForceOverflow「 和'<項目名稱=」 overlayedActionBarBackground「> @色/ overlayedActionBarDark ...'但它的工作,這是很好的)好發部位 – Malder 2013-02-26 20:43:33

+0

ForceOverflow如果您的ActionBar只能顯示兩個項目(例如,在一個小設備上),但您知道您將擁有四個項目,則需要此選項。通常其中兩個只是被切斷,但是,使用ForceOverflow,您可以獲得該圖標以獲取更多菜單圖標。 – Trinimon 2013-02-26 21:56:37

0

錯誤消息告訴你什麼是錯誤的,你的主題必須是「Theme.Sherlock,Theme.Sherlock.Light,Theme.Sherlock.Light.DarkActionBar或衍生物。」

這是發生在API 7上,但不是在API 15上,因爲API 15具有本機ActionBar支持,所以可以使用,但API 7並非如此,因此需要主題化。

0

從錯誤中去:

You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."

看來,您的自定義style不從Sherlock主題繼承。檢查values文件夾中的styles.xml文件,確保它們從其中一個文件夾繼承。