Search code examples
androidactionbarsherlockandroid-manifest

How set Theme in main activity api 7?


I offer my users a light and dark option for theming.

The error in the Log:

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

The error occurs in setContentView(R.layout.activity_main); in the 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);

I did not add something in the manifest, because the themes should be changed dynamically 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>

This error occurs on API 7. On API 15 all works very well.


Solution

  • I'm setting this in this way ...

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

    where the style definition are looking like ...

     <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>
    

    That works pretty fine. So I think you took the wrong style definition from ABS.

    Cheers!