Satan Goss
Satan Goss

Reputation: 9

Error Calling Intent

There is an error when I call the intent startActivity (new Intent (this, Advogado1.class)), How should I proceed to properly call this Intent

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

            alert.setTitle("Atenção");
            alert.setMessage("Digite o Numero da OAB");

            // Set an EditText view to get user input
            final EditText input = new EditText(this);
            alert.setView(input);

            alert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                            int oab = Integer.parseInt(input.getText()
                                    .toString());
                            // Do something with value!

                            if (oab == 1) {

                                startActivity(new Intent(this, Advogado1.class));
                            }

                        }
                    });

            alert.setNegativeButton("Cancelar",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // Canceled.
                        }
                    });

            alert.show();

Upvotes: 0

Views: 325

Answers (1)

The call to,

startActivity(new Intent(this, Advogado1.class));

should not use 'this', it should use,

startActivity(new Intent(NameOfYourActivity.this, Advogado1.class)); 

because this refers to the anonymous class extending DialogInterface.OnClickListener, and not your Activity class. The Intent needs the caller class to be an instance of Activity.

Upvotes: 2

Related Questions