Claudio Castro
Claudio Castro

Reputation: 191

android - Alert Dialog + 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(this,Screen2Activity.class);
            startActivity(intent);
        }
    }
}

I want to start an activity from an AlertDialog but i found an error on Intent intent = new Intent(this,Tela2Activity.class);

screen2 is linked to a 'onclick' of a button

Upvotes: 1

Views: 12895

Answers (1)

Thomas Vos
Thomas Vos

Reputation: 12571

Change this part of the code:

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

To this:

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

Full code:

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);

}

Upvotes: 3

Related Questions