57

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 {
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Doug Steiert
  • 625
  • 2
  • 6
  • 12

8 Answers8

127

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I will try this, although, it does like very similar to something I have tried. Sorry, I should've posted a chunk of code where I didn't use "Theme.NoTitleBar..." – Doug Steiert Sep 21 '16 at 00:02
  • I just want to state that I had to remove the "android:theme=@style/AppFullScreenTheme" from under the Activity section. With it there, it caused breakage of the app due to activity not assignable. – Doug Steiert Sep 21 '16 at 16:04
  • What do you mean "not assignable"? You had to add the `AppFullScreenTheme` style in the `styles.xml`, then you could use that in the Manifest as written – OneCricketeer Sep 21 '16 at 16:53
  • 1
    The video I followed told me to add `android:theme="@android:style/Theme.NoTitleBar.Fullscreen" ` under the ` – Doug Steiert Sep 21 '16 at 18:05
  • Was he extending `AppCompatActivity`? – OneCricketeer Sep 21 '16 at 18:06
  • He was extending `Activity`, as he was using a much older version of AS, while I did extend `AppCompatActivity`. – Doug Steiert Sep 21 '16 at 18:38
  • 1
    Version of Android Studio shouldn't matter. You got an error because you were extending `AppCompatActivity`, which requires an `AppCompat` theme. The video was not getting an error because it used regular `Activity`, which does not need those themes. – OneCricketeer Sep 21 '16 at 19:34
  • Ahh..no other answers seemed to describe that issue clearly, which left me very confused for a long time. Thank you for the help, and I just marked it - sorry, had to figure out how. – Doug Steiert Sep 21 '16 at 20:04
44

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

Ya Si
  • 819
  • 7
  • 12
  • 3
    Uhhh, this was for me as well. I was passing in the App context instead of the Activity context. Error message does not indicate this is the problem. Thank you. – Johnny Jan 10 '20 at 22:13
  • Thanks, I was using dialogbox on onClickListener and was passing the incorrect context. – Kashif Saleem Jan 22 '20 at 15:07
  • I have the same reason but I'm passing the right context. Can't solve the problem – Jones Mar 15 '20 at 21:58
  • Try another approach - use fragment context in MaterialAlertDialog from the material library. val dialog = MaterialAlertDialogBuilder(context) .setView(customLayout) .show() – Ya Si Mar 17 '20 at 13:29
  • 1
    Thanks a lot! It's strange the getContextApplication() wasn't working neither the keyword(this), but with the class worked without problem. – Dam Jul 06 '21 at 00:57
  • Tahnks a lot, you saved my day! I faced the same issue and that was the main reason for my crash. – Othmane_lam Feb 21 '23 at 15:47
10

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

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

or each kind of AppCompat theme!

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
5

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

Y.E.S.
  • 764
  • 5
  • 16
2

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">
eriel marimon
  • 1,230
  • 1
  • 19
  • 29
1

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.

1

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

Entire AndroidManifest.xml file

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.rv_supplier"
  xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.front" android:required="false" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- It's necessary because of react-native-qrcode-scanner -->
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">

      <activity
        android:name=".SplashActivity"
        android:theme="@style/BootTheme"
        android:label="@string/app_name"
        android:exported="true">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      
      <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_notification" />

      <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/notification_ic"
        tools:replace="android:resource" />
    </application>
</manifest>

```
Murilo Dutra
  • 151
  • 1
  • 5
0

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