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

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.


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

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;