Kiryl Bielašeŭski
Kiryl Bielašeŭski

Reputation: 2683

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.

Upvotes: 1

Views: 1092

Answers (3)

Steven Byle
Steven Byle

Reputation: 13269

Going from the error:

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

It seems that your custom style does not inherit from a Sherlock theme. Check the styles.xml file in your values folder and ensure they inherit from one of those.

Upvotes: 0

Trinimon
Trinimon

Reputation: 13957

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!

Upvotes: 1

Brent Hronik
Brent Hronik

Reputation: 2427

The error message tells you what is wrong, your theme must be "Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."

This is occuring on API 7 but not on API 15 because API 15 has native ActionBar support, so that is used, while this is not the case on API 7, and hence the need for the theming.

Upvotes: 0

Related Questions