How to fix: "You need to use a Theme.AppCompat theme (or descendant) with this activity"

AndroidAndroid AppcompatAppcompatactivity

Android Problem Overview


I am having trouble running my Android app in a fullscreen mode per instructions of a video. When it tries to run, the app crashes with the error.

"You need to use a Theme.AppCompat theme (or descendant) with this activity

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.djsg38.hikerswatch">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Styles File

<resources>

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

Partial MainActivity that may be useful

public class MainActivity extends AppCompatActivity {

Android Solutions


Solution 1 - Android

Your application has an AppCompat theme

<application
    android:theme="@style/AppTheme">

But, you overwrote the Activity (which extends AppCompatActivity) with a theme that isn't descendant of an AppCompat theme

<activity android:name=".MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

You could define your own fullscreen theme like so (notice AppCompat in the parent=)

<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Then set that on the Activity.

<activity android:name=".MainActivity"
    android:theme="@style/AppFullScreenTheme" >

Note: There might be an AppCompat theme that's already full screen, but don't know immediately

Solution 2 - Android

Used to face the same problem. The reason was in incorrect context passing to AlertDialog.Builder(here). use like AlertDialog.Builder(Homeactivity.this)

Solution 3 - Android

u should add a theme to ur all activities (u should add theme for all application in ur <application> in ur manifest) but if u have set different theme to ur activity u can use :

 android:theme="@style/Theme.AppCompat"

or each kind of AppCompat theme!

Solution 4 - Android

If you add the android:theme="@style/Theme.AppCompat.Light" to <application> in AndroidManifest.xml file, problem is solving.

Solution 5 - Android

This Error comes, when you use a AlertDialog in an activity. If you want get solution of this problem, you just need to make sure that in which activity you wanted to show the AlertDialog popup, this context must be like Demo.this. never use the getApplicationContext.

Solution 6 - Android

I was getting this error while trying to setup react-native-bootsplash, in their documentation they say you have to change you application theme from this

<application
     ...
     android:theme="@style/AppTheme">

to this

<application
      ...
      android:theme="@style/BootTheme">

But what worked for me was to leave the application theme as android:theme="@style/AppTheme" and change the activity theme, like that:

<activity
        android:name=".SplashActivity"
        android:theme="@style/BootTheme"/> <!-- HERE -->

Solution 7 - Android

Faced same issue. If your application is using Theme.MaterialComponents...... then other place if using @android:style/Theme.... then this issue faced.

Solution 8 - Android

Note that there may be 2 styles.xml in /values/ and /values-night/, in your project, for when the OS setting is set to Light or Dark mode. In both you need to replace the default Theme with an AppCompat Theme in case you have the Dark setting on. Sometimes is only ON at night, so your app may compile during the day but stop working after certain hour, causing a lot of debugging confusion and headache. This also causes it to fail only in some devices (sim vs real for example):

old

<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">

new

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug SteiertView Question on Stackoverflow
Solution 1 - AndroidOneCricketeerView Answer on Stackoverflow
Solution 2 - AndroidYa SiView Answer on Stackoverflow
Solution 3 - AndroidSana EbadiView Answer on Stackoverflow
Solution 4 - AndroidY.E.S.View Answer on Stackoverflow
Solution 5 - AndroidProgrammer TonyView Answer on Stackoverflow
Solution 6 - AndroidMurilo DutraView Answer on Stackoverflow
Solution 7 - AndroidSujit AcharyaView Answer on Stackoverflow
Solution 8 - Androideriel marimonView Answer on Stackoverflow