You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Theme.AppCompat causes other error

AndroidAndroid Appcompat

Android Problem Overview


I use appcompat v22.1.0 in my App and use Toolbar. Everything was fine when I use Theme.AppCompat.Light.NoActionBar. When I start implement AlertDialog, it produce error like this:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:47)
        at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:92)
        at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:882)
        at com.ramabmtr.map.findingmoo.MainActivity.onOptionsItemSelected(MainActivity.java:216)
        at android.app.Activity.onMenuItemSelected(Activity.java:2572)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:74)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:164)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:740)
        at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:598)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16989)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4812)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
        at dalvik.system.NativeStart.main(Native Method)

Based on that error, I change my theme to Theme.AppCompat and put this:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

to my theme. But it produce the same error.

Style.xml (old)

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
</resources>

Style.xml (new)

<resources>
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
</resources>

Anyone knows how to fix it??

MainActivity.java

package com.ramabmtr.map.findingmoo;

import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TextView toolbarTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/panpizza.ttf");
    toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
    toolbarTitle.setTypeface(myTypeface);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setTitle(R.string.filter_title);
    builder.setMessage("test");
    builder.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}
}

Android Solutions


Solution 1 - Android

Fixed my problem by using MainActivity.this (or YourActivityName.this)

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

Make sure you already Theme.AppCompat and extending AppCompatActivity.

Solution 2 - Android

Basically your Activity is using Toolbar (which replaces Action Bar) so you need to use style for the Activity that has no Action Bar like Theme.AppCompat.Light.NoActionBar. If you have your own style for dialog then you need to inherit the proper AppCompat theme.

<style name="myDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    ...
</style>

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));

Solution 3 - Android

In my case, this crash was caused because I was passing View.getContext().getApplicationContext() as Context to the Builder. This was fixed by using getActivity() instead.

Solution 4 - Android

Saw this same exception using Activity and Theme.Light theme. My problem was a wrong import, I was using the support one. import android.support.v7.app.AlertDialog; instead of import android.app.AlertDialog;

Solution 5 - Android

if you have this error when you creating a dialog (just in my case) you should use the following:

AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light);

instead of:

AlertDialog.Builder dialog = new AlertDialog.Builder(context);

This worked me perfectly!

Solution 6 - Android

The probem is because of Context which You are passing to build the Alert Dialog.Don't Use getApplicationContext().Try using your Activity context.Use AlertDialog.Builder builder = new AlertDialog.Builder(MainActiviy.this);

Solution 7 - Android

This work for me... after read several answer was...

Change my import like this:

import android.app.AlertDialog;

instead of

import android.support.v7.app.AlertDialog; 

this give error Unable to add window -- token null is not for an application... so I change the context of the builder from

AlertDialog.Builder builder = new 

AlertDialog.Builder(getAplicationContext()); to

AlertDialog.Builder builder = new AlertDialog.Builder(*MainActivity.this*);

Solution 8 - Android

Adding Android:theme="@style/Theme.AppCompat" like this in manifest

<activity
    android:theme="@style/Theme.AppCompat"
    android:name=".MainActivity"

solved the problem

Solution 9 - Android

If you are using support library your activity extends AppCompactActivity, if you use android studio to create activity this is default. In such case pass context to the builder as ActivityName.this or simply this if you are passing it in onCreate, passing getApplicationContext() will not work.

This is my style using appcompact

<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>
        <item name="android:textSize">18sp</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

And everything is working fine when I use this or ActivityName.this as mentioned above (here ActivityName is the name of your current activity i.e. MainActivity.this).

If you are using in fragment you should pass getActivity() as context to builder instead of getContext().

Solution 10 - Android

In kotlin this worked to me:

val dialog = AlertDialog.Builder(this)

Solution 11 - Android

You need to pass your Activity's context instead to pass your Context. Try with "this" instead "context". This work for me

Solution 12 - Android

In my case, I had to check and make sure that all styles.xml files use Theme.AppCompat. The standard styles.xml file had that theme but there were also styles(v21).xml, styles(v11).xml and styles(sw600dp).xml that were auto-generated. The simple solution would be to copy and paste the customized default AppTheme style into these folders.

All the best!

Solution 13 - Android

Please be careful with such problem, i've waisted long long hours on such bug. it happens only on android 4.4.2 version, despite the fact that i'm using the Appcompat theme everywhere, tried to play with all styles and use many other threads and answers.

the problem was caused by this line:

 <style name="LooperLabTheme"
        parent.theme="@style/Theme.AppCompat.Light.NoActionBar"
        theme="@style/Theme.AppCompat.Light.NoActionBar">
<style/>

please notice the "parent.theme", i've copied it from some answer in the first days of launching the project that i'm woking on, it should be "parent" without the theme.

 <style name="LooperLabTheme"
            parent="@style/Theme.AppCompat.Light.NoActionBar"
            theme="@style/Theme.AppCompat.Light.NoActionBar">
    <style/>

I just want to help others who are running from thread to thread to find out the fix for his very specific problem, it might be the same as mine, save your time.

Solution 14 - Android

getSupportActionBar().getThemedContext()

AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext(), android.R.style.Theme_Material_Dialog_Alert);
} else {
    builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext());
}
builder.setTitle("Alert Dialog")
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
       .setIcon(android.R.drawable.ic_dialog_alert)
       .show();

Solution 15 - Android

Just put your activity here:

 AlertDialog.Builder builder = new AlertDialog.Builder(your_activity.this);

instead of

 AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

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
QuestionRama BramantaraView Question on Stackoverflow
Solution 1 - AndroidmariozawaView Answer on Stackoverflow
Solution 2 - AndroidinmythView Answer on Stackoverflow
Solution 3 - AndroidBinoy BabuView Answer on Stackoverflow
Solution 4 - AndroidRogelioView Answer on Stackoverflow
Solution 5 - AndroidnarancsView Answer on Stackoverflow
Solution 6 - AndroidRajesh.kView Answer on Stackoverflow
Solution 7 - AndroidCarolinaView Answer on Stackoverflow
Solution 8 - AndroidGoGamView Answer on Stackoverflow
Solution 9 - AndroidInzimam Tariq ITView Answer on Stackoverflow
Solution 10 - AndroidhertzonView Answer on Stackoverflow
Solution 11 - AndroidoskarkoView Answer on Stackoverflow
Solution 12 - AndroidiOSAndroidWindowsMobileAppsDevView Answer on Stackoverflow
Solution 13 - AndroidBahadin KhaliliehView Answer on Stackoverflow
Solution 14 - AndroidmiragesseeView Answer on Stackoverflow
Solution 15 - Androidheet kanabarView Answer on Stackoverflow