首页 > 解决方案 > 如何解决:IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)

问题描述

我的应用程序无法在模拟器中运行。引用的错误是 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this Activity。

我已经阅读了许多类似的问题,包括: .IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

ActionBarCompat: java.lang.IllegalStateException: 你需要使用 Theme.AppCompat

但这些都没有奏效。

res>values>styles.xml 文件中的代码是:

<resources>

    <style name = "AppBaseTheme" parent = "TextAppearance.AppCompat">
        <item name = "android:colorPrimary">@android:color/holo_blue_bright</item>
        <item name = "android:colorPrimaryDark">@android:color/holo_blue_dark</item>
        <item name = "android:colorAccent">@android:color/holo_red_dark</item>


    </style>



</resources>

src>main>AndroidManifest.xml 文件中的代码为:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppBaseTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果有人可以提供帮助,我将不胜感激:)

标签: javaandroid

解决方案


应用程序主题不能<style name = "AppBaseTheme" parent = "TextAppearance.AppCompat">,因为TextAppearance主题根本不能用于应用程序主题。因此,请使用 anyTheme.AppCompat作为应用程序的默认主题。为此,我可以举一个例子:

<style name = "AppBaseTheme" parent = "Theme.AppCompat">

或者

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

等等

希望它会起作用


推荐阅读