Appcompat在Lollipop上崩溃了

时间:2015-01-13 12:13:08

标签: android android-5.0-lollipop android-appcompat

我创建了一个应用程序,它使用appcompat v21库来获取棒棒糖前设备上的素材。这个应用程序曾经工作,直到我将手机升级到Lollipop。现在它在我运行时崩溃了。
错误消息是:

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

但是,据我所知,我到处都在使用AppCompat主题。

活动布局:     

    <include layout="@layout/toolbar"/>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:foreground="?android:windowContentOverlay"/>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="480dp"
            android:layout_height="fill_parent"
            android:layout_gravity="start"
            android:background="@color/primaryBackground"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:listSelector="@color/listSelectedItem" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="android:activatedBackgroundIndicator">@drawable/list_selector</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

    <style name="MyActionBar" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">@color/primaryColor2</item>
        <item name="android:windowActionBarOverlay">true</item>
        <!-- Support library compatibility -->
        <item name="background">@color/primaryColor2</item>
        <item name="windowActionBarOverlay">true</item>
    </style>
</resources>

我做错了什么?

干杯, CJ

编辑: 添加了清单          

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <application
        android:name=".MusicViewerApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/myAppTheme" >
        <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>
        <activity
            android:name=".SetlistConcertActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_setlist"
            android:screenOrientation="portrait" >
        </activity>

    </application>

</manifest>

Edit2:添加了样式-v21

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myAppTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:statusBarColor">@color/primaryColorDark</item>
        <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="android:activatedBackgroundIndicator">@drawable/list_selector</item>

    </style>



    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

</resources>

1 个答案:

答案 0 :(得分:1)

你需要确保你的主题都使用appCompat,不要在v21资源中使用材质主题,这是崩溃的原因,只需在v21中使用appCompat主题,你的崩溃就会消失。