Customizing title bar in Android. Getting Exception You need to use a Theme.AppCompat theme

StackOverflow https://stackoverflow.com/questions/23211779

  •  07-07-2023
  •  | 
  •  

Question

Search a lot but nothing found useful.

I am creating a simple app with my customized Title bar but not getting so far from days. I am using this https://developer.android.com/training/basics/actionbar/styling.html.

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.title"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:theme="@style/CustomActionBarTheme"//title bar
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.title.MainActivity"
            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>

</manifest>

I have created theme.xml like res/values/themes.xml.

Code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">#CCCCCC</item>

        <!-- Support library compatibility -->
    </style>
</resources>

Getting Exception :( logcat:

04-22 11:37:21.755: E/test(11652): Exception
04-22 11:37:21.757: E/AndroidRuntime(11652): FATAL EXCEPTION: main
04-22 11:37:21.757: E/AndroidRuntime(11652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.title/com.example.title.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.os.Looper.loop(Looper.java:153)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.main(ActivityThread.java:5299)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at dalvik.system.NativeStart.main(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at com.example.title.MainActivity.onCreate(MainActivity.java:18)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.Activity.performCreate(Activity.java:5182)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 11:37:21.757: E/AndroidRuntime(11652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-22 11:37:21.757: E/AndroidRuntime(11652):    ... 11 more

Guide me with this please

Was it helpful?

Solution

Your MainActivity probably extends from ActionBarActivity. And you are probably referencing AppCompat. So you need to use Theme.AppCompat.

Also you have android:minSdkVersion="11". So there is no need to extend ActionBarActivty and no need to reference AppCompat and no need for Theme.AppCompat

http://developer.android.com/guide/topics/ui/actionbar.html

Also check

https://developer.android.com/training/basics/actionbar/styling.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top