使用Theme.Material.Light.DarkActionBar与Theme.AppCompat.Light.DarkActionBar崩溃Android L上的应用程序(api v21)

lov*_*esh 4 android android-appcompat material-design

这是我的资源的样子

res/values/styles.xml
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

就像它在这里说的那样

res/values-v21/styles.xml
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
    </style>
Run Code Online (Sandbox Code Playgroud)

和AndroidManifest.xml有

<application android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme" >
Run Code Online (Sandbox Code Playgroud)

在我的MainActivity.java中,我从ActionBarActivity继承

public class MainActivity extends ActionBarActivity {
....
...
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在KitKat(API 19)上运行应用程序时,应用程序运行,我看到Dark Action栏和材料设计但是当我在Android L(API 21)上运行时,应用程序崩溃说java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity..我知道这是因为我继承了ActionBarActivity但是如果我使用appcompat主题values-v21/styles.xml那么我将如何使用材料主题?

GPa*_*ack 6

Theme.AppCompat如果在Lollipop设备上运行,则继承自Material到Platform主题:res/values-v21/styles.xml在这种情况下不是必需的.