"您需要在此活动中使用Theme.AppCompat主题(或后代)"使用AppCompat主题时

时间:2015-05-10 13:11:02

标签: android android-support-library android-theme android-compatibility

我的应用程序存在一个非常奇怪的问题。更新到最新版本的支持库后,我收到此错误:

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:

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

为什么抛出这个异常呢?

修改

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>

1 个答案:

答案 0 :(得分:0)

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

更新即可
implementation 'androidx.appcompat:appcompat:1.0.0'

implementation 'androidx.appcompat:appcompat:1.0.2'