2
votes

I am trying to use alert dialog on delete button. But It is showing exception to show the dialog. When I click on delete it crashes and shows exception on .show.

I tried to use Theme.AppCompat theme for this activity but still it crashes.

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


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(getApplicationContext())
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                Toast.makeText(getApplicationContext(), "Yaay", Toast.LENGTH_SHORT).show();
                                i = new Intent();
                                db.deleteEvent(eventData);
                                Log.i("d", "delete");
                                setResult(RESULT_OK, i);
                                finish();

                                // continue with delete
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        });

My Theme

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

        <item name="windowActionBarOverlay">false</item>

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

        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@color/background_material_light</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

Thank you.

1
I think the exception is BadTokenException. You need to dismiss the alert dialog on NO and DELETE buttonAziz
onClick() of button?Sid
onClick of setPositiveButton and setNegativeButtonAziz

1 Answers

5
votes

u are using the wrong context you have to use the activity context and not the application context

change:

  new AlertDialog.Builder(getApplicationContext())

to

  new AlertDialog.Builder(YourActivityName.this)