[Android]新建项目使用AppCompatActivity后运行闪退

报错

日志:

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

FATAL EXCEPTION: main
Process: com.example.gatestdemol, PID: 26071
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gatestdemol/com.example.gatestdemol.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2896)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2974)
	at android.app.ActivityThread.-wrap11(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:192)
	at android.app.ActivityThread.main(ActivityThread.java:6744)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:525)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
	at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:926)
	at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889)
	at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:585)
	at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:153)
	at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1291)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2878)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2974) 
	at android.app.ActivityThread.-wrap11(Unknown Source:0) 
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675) 
	at android.os.Handler.dispatchMessage(Handler.java:106) 
	at android.os.Looper.loop(Looper.java:192) 
	at android.app.ActivityThread.main(ActivityThread.java:6744) 
	at java.lang.reflect.Method.invoke(Native Method) 
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:525) 
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825) 

解决

这个错误是因为您的 MainActivity 使用了 AppCompatActivity,但是应用的主题没有继承自 Theme.AppCompat。AppCompatActivity 需要您的应用使用兼容库提供的主题。

要解决这个问题,请确保在您的应用资源中,为 MainActivity 设置的主题是 Theme.AppCompat 的一个子类。

在 res/values/themes.xml 中修改主题继承于Theme.AppCompat.Light.NoActionBar

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--    
        <style name="Theme.GATestDemol" parent="android:Theme.Material.Light.NoActionBar" />
    -->
    <style name="Theme.GATestDemol" parent="Theme.AppCompat.Light.NoActionBar" />

</resources>

然后,在 AndroidManifest.xml 文件中,确保 MainActivity 使用了这个主题:

<activity android:name=".MainActivity"
    android:theme="@style/AppTheme">
    ...
</activity>

猜你喜欢

转载自blog.csdn.net/u012881779/article/details/134378133
今日推荐