Java.lang.IllegalStateException You need to use Theme.AppCompat (or child) theme with this action

Greetings to my development friends!

I created a demo application for testing material design for minSdk 14 and targetSdj 23. I have the following files below. It works fine under 21 APIs. I believe that the culprit is Styles-v21.xml , because the application worked fine before that.

Its simple setup is just one action. I am testing the Material theme and the AppCompat theme to work with the following scenario.

  • Theme.AppCompat for APIs below 21.
  • Subject. Material for API 21 and 21+.

The application works perfectly under API 21, but above 21+ the application crashes: "Unfortunately, the Test material has stopped."

The error is as follows:

FATAL EXCEPTION: main
        Process: net.slidenerd.materialtest, PID: 28204
        java.lang.RuntimeException: Unable to start activity ComponentInfo{net.slidenerd.materialtest/net.slidenerd.materialtest.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:340)
        at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at net.slidenerd.materialtest.MainActivity.onCreate(MainActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

MainActivity.java

package net.slidenerd.materialtest;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>

</resources>

< > -v21.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorAccent</item>

    </style>

</resources>

Manifest.xml

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

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

        <activity android:name=".MainActivity"
        android:theme="@style/AppTheme"
        >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

. , Activity AppCompatActivity, API 14+, API 23. .

, , .

. , MainActivity AppCompatActivity, API 21 .

- , , ?

!

+4
2

AppCompatActivity, , Theme.AppCompat theme (or descendant).

, AppCompatActivity, java.lang.IllegalStateException.

, !

+4

, AppCompatActivity , , (, android: colorPrimary, android: colorSecondary ..). , , , RuntimeException. Theme.AppCompat .

. Activity Theme.AppCompat , , AppCompatActivity.

, AppCompatActivity, Theme.AppCompat , . , , , .

+1

All Articles