【问题标题】:Theme Error - You need to use a Theme.AppCompat theme (or descendant) with this activity [duplicate]主题错误 - 您需要在此活动中使用 Theme.AppCompat 主题(或后代)[重复]
【发布时间】:2018-11-27 15:20:55
【问题描述】:

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

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)

这是我的AndroidManifest.xml,其中定义了具有相应主题的所有活动。

<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>

我认为这与Theme 有关,但无法弄清楚。

【问题讨论】:

    标签: android android-theme android-styles


    【解决方案1】:

    在您的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"
    }
    

    【讨论】:

    • 它是一样的,但只是用 NoActionBar 而不是 DarkActionBar。可以吗?我不需要操作栏,因为我自己创建了一个。
    • 我看到我的主题是红色的,我不知道为什么?
    • 依赖的事情我也已经做过了
    • 请查看the documentation 以检查是否缺少更多内容。
    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 2018-06-05
    • 2015-11-27
    • 2016-07-04
    • 2016-05-16
    相关资源
    最近更新 更多