“您需要在此活动中使用Theme.AppCompat主题(或后代)"使用AppCompat主题时 [英] "You need to use a Theme.AppCompat theme (or descendant) with this activity" when using AppCompat theme

查看:114
本文介绍了“您需要在此活动中使用Theme.AppCompat主题(或后代)"使用AppCompat主题时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个非常奇怪的问题.更新到最新版本的支持库后,出现此错误:

I have a very strange problem with my app. After updating to the newest versions of Support libraries I get this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

这是引发异常的地方:

public class MainActivity extends AppCompatActivity {
    …
    @Override
        protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);//Exception thrown here
    }

    …

}

奇怪的是我在所有活动中都使用AppCompat主题: AndroidManifest.xml:

What is strange is that I am using AppCompat theme with all my Activities: AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="com.orm.SugarApp">
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    …
</application>

styles.xml:

<resources>

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

        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">@color/primary</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">@color/accent</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="windowActionBarOverlay">true</item>

    </style>

    <style name="MyActionBar"
        parent="Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:icon">@drawable/ic_launcher</item>
        <item name="icon">@drawable/ic_launcher</item>

    </style>

    <style name="MyActionBarTabText" parent="Widget.AppCompat.ActionBar.TabText">
        <item name="textAllCaps">false</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:textSize">16sp</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <style name="menu_labels_style">
        <item name="android:background">@drawable/fab_label_background</item>
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

那么为什么会抛出该异常?

Why is this exception thrown then?

v21/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="android:colorPrimary">@color/primary_lolipop</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="android:colorAccent">@color/accent</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:colorButtonNormal">@color/accent</item>
        <item name="android:textColorAlertDialogListItem">@color/accent</item>

        <item name="android:navigationBarColor">@color/navbar</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:windowActionBarOverlay">true</item>

        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>
</resources>

推荐答案

当我迁移到androidx时遇到同样的问题.我已经解决了,只需从

I have same problem when I've migrate to androidx. I've solve this by simply updating from

implementation 'androidx.appcompat:appcompat:1.0.0'

implementation 'androidx.appcompat:appcompat:1.0.2'

这篇关于“您需要在此活动中使用Theme.AppCompat主题(或后代)"使用AppCompat主题时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆