调用意图时出错

时间:2011-05-10 20:39:44

标签: android

我在调用意图startActivity (new Intent (this, Advogado1.class))时出错,我该如何正确调用此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();

1 个答案:

答案 0 :(得分:2)

致电,

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

不应该使用'this',它应该使用,

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

因为this指的是扩展DialogInterface.OnClickListener的匿名类,而不是Activity类。 Intent需要调用者类是Activity的实例。