简体   繁体   中英

Action Bar Background Not Changing

I have went on android and followed their instructions to style the Action Bar and it is not working. The app keeps stopping. I can see the changed color briefly as the message is shown. I have a note 2 with 4.4.2.. Here are my

styles.`

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

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

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="funnestgames.actionbar" >

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

and my logcat.

    5-29 20:35:33.541      838-838/funnestgames.actionbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: funnestgames.actionbar, PID: 838
java.lang.RuntimeException: Unable to start activity     ComponentInfo{funnestgames.actionbar/funnestgames.actionbar.MainActivity}:     java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or     descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)

at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

at  android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)

at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)

at funnestgames.actionbar.MainActivity.onCreate(MainActivity.java:16)

at android.app.Activity.performCreate(Activity.java:5451)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
           
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            
at android.app.ActivityThread.access$900(ActivityThread.java:175)
            
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
           
 at android.os.Handler.dispatchMessage(Handler.java:102)
            
at android.os.Looper.loop(Looper.java:146)
            
at android.app.ActivityThread.main(ActivityThread.java:5602)
            
at java.lang.reflect.Method.invokeNative(Native Method)
            
at java.lang.reflect.Method.invoke(Method.java:515)
            
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            
at dalvik.system.NativeStart.main(Native Method)

You can change the background color by following from your mainActivity class

ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#C71585")));

or it will not give you error if you use following style for your actionbar

<style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM