错误:获取AppCompatActivity与android:Theme.Material不兼容

时间:2015-07-12 22:53:23

标签: android material-design runtimeexception appcompatactivity

我正在尝试使用Material Theme为我的应用添加导航抽屉。关注this tutorial here to start

输出应该看起来像this GIF

看起来 AppCompatActivity与android:Theme.Material 不兼容,不明白为什么?

我的res/styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<!-- your theme inherits from the material theme -->
<style name="MaterialTheme" parent="android:Theme.Material">
    <!-- theme customizations -->
</style>

我的活动代码,只是扩展AppCompatActivity

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setContentView(R.layout.mainactivity);
......

现在,我的清单,不是我想要使用的:MaterialTheme

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

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

这是我得到的错误:

java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.focusonanywhere.www.focusonanywhere/com.focusonanywhere.www.focusonanywhere.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 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      com.focusonanywhere.www.focusonanywhere.MainActivity.onCreate(MainActivity.java:45)
        at android.app.Activity.performCreate(Activity.java:5990)
        at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at            android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at          com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

  

看起来AppCompatActivity与android:Theme.Material不兼容,不明白为什么?

appcompat-v7操作栏后退需要使用Theme.AppCompat及其子主题(例如Theme.AppCompat.Light)。那就是那个图书馆的本质。您的选择是:

  1. 不使用AppCompatActivity并使用原生操作栏或

  2. 不使用Theme.Material而是使用Theme.AppCompat