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

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

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.

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.

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;

Karoly

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!

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

Carolina

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*);
GoGam

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

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

solved the problem

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.

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

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

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!

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.

miragessee
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();
hertzon

In kotlin this worked to me:

val dialog = AlertDialog.Builder(this)