android - 警报对话+意图

时间:2015-03-19 14:41:43

标签: android android-intent alertdialog android-alertdialog

public void screen2 (View view) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("screen2");
    builder.setMessage("go to screen2?");

    builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Intent intent = new Intent(this,Screen2Activity.class);
            startActivity(intent);
        }
    }
}

我想从AlertDialog开始活动,但我在Intent intent = new Intent(this,Tela2Activity.class);

上发现错误

screen2链接到' onclick'一个按钮

1 个答案:

答案 0 :(得分:3)

更改此部分代码:

Intent intent = new Intent(this,Screen2Activity.class);
startActivity(intent);

对此:

Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
startActivity(intent);

完整代码:

public void screen2 (View view) {

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("screen2");
builder.setMessage("go to screen2?");

builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
        startActivity(intent);

}