“You need to use a Theme.AppCompat theme (or desce

2019-03-06 03:21发布

问题:

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.

This is where the exception is thrown:

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

    …

}

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?

EDIT:

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:

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'

to

implementation 'androidx.appcompat:appcompat:1.0.2'