-2

I created an AlertDialog that shows 3 buttons in Android Studio but an error appears on the reserved word "this". In the attached image, the source code can be viewed in greater detail.

Android Studio Screenshot

How can I solve this error?

private void muestraDialogo2() {

    AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
    builder2.setTitle("Lista de muestra");
    builder2.setMessage("Tipos de muestra");
    builder2.setCancelable(false);

    builder2.setPositiveButton("Imprimir todas", new DialogInterface.OnClickListener() {


        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(Main3Activity.this, "Imprimiendo...*", Toast.LENGTH_SHORT).show();

        }


    });
robinCTS
  • 5,746
  • 14
  • 30
  • 37
Airbeat
  • 11
  • 2

1 Answers1

1

You're passing the wrong parameter in the AlertDialog.Builder constructor. at line 50, the this object refers to a View.OnClickListener class and not a valid context object.

Change the line:

AlertDialog.Builder builder2 = new AlertDialog.Builder(this);

for

AlertDialog.Builder builder2 = new AlertDialog.Builder(Main3Activity.this);
Ariel Carbonaro
  • 1,529
  • 1
  • 13
  • 25