当前位置: 首页 > 知识库问答 >
问题:

主题错误-您需要在此活动中使用主题.AppCompat主题(或后代)[重复]

邢法
2023-03-14

我在启动应用程序时遇到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.shantanu.fireaudit, PID: 24098
                  java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
                      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                      at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
                      at android.support.v7.app.AlertController.installContent(AlertController.java:231)
                      at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:278)
                      at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
                      at android.app.Dialog.show(Dialog.java:293)
                      at com.example.shantanu.fireaudit.MainGroupActivity$1.onClick(MainGroupActivity.java:79)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
<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/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".GuidelinesActivity" />
    <activity android:name=".FormActivity" />
    <activity android:name=".MainGroupActivity"/>
    <activity android:name=".LocationsActivity"/>
    <activity
        android:name=".Main2Activity"
        android:label="@string/title_activity_main2"
        android:theme="@style/AppTheme"></activity>
</application>

我认为这与主题有关,但想不通。

共有1个答案

公冶京
2023-03-14

res/values/style.xml中,AppTheme样式应该是Theme.AppCompat主题。例如:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

还要确保您正在app/build.gradle文件中导入android appcompat库。

dependencies {
    implementation "com.android.support:appcompat-v7:27.1.1"
}
 类似资料: