如何更改Android Action Bar的颜色?

时间:2015-03-17 15:34:39

标签: android android-layout android-actionbar

我正在尝试更改Android ActionBar的颜色,但我的应用每次关闭都会出错。我在其他帖子中尝试了所有其他建议和修正,但它们似乎对我不起作用。还 - minSdkVersion 19

错误:

03-17 11:25:56.884  11999-11999/ca.holdfastonline.menu_test_02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ca.holdfastonline.menu_test_02, PID: 11999
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.holdfastonline.menu_test_02/ca.holdfastonline.menu_test_02.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)

My Styles.xml

<resources>
    <style name="QueryTheme" parent="@style/Theme.AppCompat">
        <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
    <!-- 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">#2980b9</item>
    </style>
</resources>

我的AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/QueryTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/CustomActionBarTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

谢谢。

2 个答案:

答案 0 :(得分:1)

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

您只有一项活动。它正在使用@style/CustomActionBarTheme。您对CustomActionBarTheme的定义是继承自Theme.Holo.Light.DarkActionBar。这不是AppCompat主题。将Theme.Holo.Light.DarkActionBar更改为Theme.AppCompat.Light.DarkActionBar

答案 1 :(得分:1)

这可能是因为在你的Java文件中你正在使用ActionBarActivity而你应该使用Activity。

更多信息 基本上你使用的是ActionBarActivity,这个活动需要一个Theme.AppCompact,它就是你的错误所在。要解决此问题,您需要更改为使用常规Activity或仅使用Theme.AppCompact

相关问题