0

I want to display an Alert Dialog for the user when he presses on the delete ImageButton. However, the app crashes when the button is pressed.

This is the code inside the Adapter for the onClickListener:

holder.delete.setOnClickListener(new View.OnClickListener() {
            String productId = String.valueOf(item.getId());
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(mCtx);
                alertDialog.setTitle("App Name");
                alertDialog.setMessage("Are you sure you want to delete this item?");
                alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteItem(productId);
                        dialog.cancel();
                    }
                });
                alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                    }
                });

                AlertDialog dialog = alertDialog.create();
                dialog.show();
            }
        });

This is what my LogCat looks like : The error is in this line :

AlertDialog dialog = alertDialog.create();

enter image description here

EngEl
  • 19
  • 1
  • 9
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Oct 10 '21 at 19:08
  • You're setting a local variable instead of the field. Remove AlertDialog definition – Amin Oct 10 '21 at 19:16
  • No, there is nothing shown in Logcat. It only writes that the app is DEAD – EngEl Oct 10 '21 at 19:18
  • i highly doubt that, do you have logcat setup to the correct device ? post a picture – a_local_nobody Oct 10 '21 at 19:22
  • Yeah I don't know, I have added the image to question – EngEl Oct 10 '21 at 19:28
  • that process you're trying to debug isn't there anymore. start your app again and on that dropdown see if there's a new option available to select, which isn't dead – a_local_nobody Oct 10 '21 at 19:32
  • Okay, I have updated the image in the question @a_local_nobody – EngEl Oct 10 '21 at 19:37
  • then it's a simple search away - https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – a_local_nobody Oct 10 '21 at 19:40
  • Check your theme of the activity; Hope this link will help you to fix the issue https://stackoverflow.com/questions/39604889/how-to-fix-you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-a/39604946 – kashyap Oct 10 '21 at 19:44

1 Answers1

1
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mCtx);

Dialogs need activity. If you pass context you will receive an error

Use interface for onclickListener and implement in your activity then pass your activity to your dialog!

farid
  • 300
  • 1
  • 8