[英]java.lang.IllegalStateException You need to use a Theme.AppCompat theme (or descendant) with this activity
Greetings my developer friends! 问我的开发者朋友们!
I created a demo app to test the material design for minSdk 14 and targetSdj 23. I have the following files below. 我创建了一个演示应用程序来测试minSdk 14和targetSdj 23的材料设计。我有以下文件。 It works fine below the 21 API. 它在21 API下工作正常。 I believe the culprit is Styles-v21.xml as the app worked fine before that. 我相信罪魁祸首是Styles-v21.xml,因为应用程序在此之前工作正常。
Its a simple setup, just one activity. 它只是一个简单的设置,只有一个活动。 I am testing the Material theme and AppCompat theme for working with the following scenario. 我正在测试Material主题和AppCompat主题以处理以下场景。
The app works fine below API 21, but above 21+ the App crashes saying,"Unfortunately materialTest has stopped." 该应用程序在API 21下运行良好,但是超过21+的应用程序崩溃说:“不幸的是,材料测试已停止。”
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 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 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>
Styles-v21.xml 样式,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 的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>
Solution : I solved this issue by extending Activity instead of AppCompatActivity and the application runs fine on both API 14+ to API 23 smoothly. 解决方案 :我通过扩展Activity而不是AppCompatActivity解决了这个问题,应用程序在API 14+到API 23上运行顺利。 But, could you justify the preferred solution to this problem. 但是,你能否证明这个问题的首选解决方案是合理的。
All the code needed to produce the error is above. 生成错误所需的所有代码都在上面。
Note : To produce the error, MainActivity should extend AppCompatActivity and you should run on API 21 or above. 注意 :要产生错误, MainActivity应扩展AppCompatActivity ,您应该在API 21或更高版本上运行。
Could anyone explain the ideal case happening here so as to understand the underlying problem? 任何人都可以解释这里发生的理想情况,以便了解潜在的问题吗?
Thank You! 谢谢!
If your activity extends from AppCompatActivity
is mandatory that your activity theme must be Theme.AppCompat theme (or descendant)
. 如果您的活动从AppCompatActivity
扩展,那么您的活动主题必须是Theme.AppCompat theme (or descendant)
。
If you want to use other theme don't extends from AppCompatActivity
and you'll stop receiving java.lang.IllegalStateException
. 如果要使用其他主题,请不要从AppCompatActivity
扩展,否则您将停止接收java.lang.IllegalStateException
。
Hope this helps!! 希望这可以帮助!!
The AppCompatActivity is supposed to be used with other components from the Compat library, and they expect that you have defined some attributes in your theme (like android:colorPrimary, android:colorSecondary, etc). AppCompatActivity应该与Compat库中的其他组件一起使用,并且他们希望您在主题中定义了一些属性(如android:colorPrimary,android:colorSecondary等)。 If these attributes are not defined in your theme, the components that need them will throw a RuntimeException. 如果未在主题中定义这些属性,则需要它们的组件将抛出RuntimeException。 Theme.AppCompat themes define these attributes. Theme.AppCompat主题定义了这些属性。
In the reverse way there is no problem. 反过来没有问题。 You can extend from Activity and use a theme from Theme.AppCompat without problems, but you will lose some functionality provided by the AppCompatActivity. 您可以从Activity扩展并使用Theme.AppCompat中的主题而不会出现问题,但是您将失去AppCompatActivity提供的一些功能。
So, if you want to use AppCompatActivity, use a Theme.AppCompat theme or define the attributes it requires in your theme. 因此,如果要使用AppCompatActivity,请使用Theme.AppCompat主题或定义主题中所需的属性。 You can go to the line that throws the exception to see which attribute is needed. 您可以转到抛出异常的行以查看所需的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.