jjharrison
jjharrison

Reputation: 871

How to start activity on button click in alertDialog

I have an alert dialog setup within a class called 'ViewBreakout' I have the button setup fine and it creates the button but when I try to add an intent I get an error message that reads "The constructor Intent(new DialogInterface.OnClickListener(){}, Class) is undefined". The solution it offers is to remove argument to match intent();???

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    dialogBuilder.setTitle("UNLUCKY :(");
    dialogBuilder.setMessage("You lost all your lives");
    dialogBuilder.setPositiveButton("try again", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Intent i = new Intent (this, main.class);//get error here
            startActivity(i);
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

Here is the code withing the class , the alertDialog is set when a global variable from another class equals 0. I am really stuck and it is for a uni project due in soon.

here is my code from the class. I thought I would show you the whole class to see if there is something im missing

    /**
    * Displays a graphical view of the game of breakout
    */


    class ViewBreakout extends View implements OnTouchListener, Observer
    {
    // private static final long serialVersionUID = 1L;
    private ControllerBreakout breakoutController;
    private GameObject       ball;
    private GameObject[]     bricks;
    private GameObject       bat;
    private int              score;
    private long             frames = 0;
    private Paint            paint  = new Paint();
    private boolean isBall = true;



    public ViewBreakout(Context context)
    {
    super(context);
    Debug.trace("View Breakout");
    setFocusable(true);
    setFocusableInTouchMode(true);      //

    this.setOnTouchListener(this);      // Take touch actions

    paint.setColor(Color.BLACK);    // Paint colour
    paint.setAntiAlias(true);       // Better quality
    if ( W < 600)
     paint.setTextSize(30);        // Text size
    else
    paint.setTextSize(40);


    }

    /**
    * Code called to draw the current state of the game Uses 
    *    paint.setColor -- set paint colour
    *     drawRect:      -- Draw rectangle 
    *    setPaint:      -- Colour used 
    *    drawText:      -- Write string on display
    */
    @Override
    public void onDraw(Canvas canvas)
    {
    frames++;


     paint.setColor(Color.DKGRAY);    // Paint colour
     canvas.drawRect(0, 0, W, H, paint);

     //if lives is 0 then display message
     if(LIVES <= 0){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    dialogBuilder.setTitle("UNLUCKY :(");
    dialogBuilder.setMessage("You lost all your lives");
    dialogBuilder.setPositiveButton("try again", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Intent i = new Intent (this, main.class);
            startActivity(i);
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

}

Upvotes: 0

Views: 2160

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

Your intent is created inside an other class, the inner class OnClickListener. So "this" refers to the instance of your anonymous inner class OnClickListener.

Change this by :

Intent i = new Intent (MyActivity.this, main.class);

EDIT :

//Add a context variable

private Context myContext;

public ViewBreakout(Context context){ 
       //your stuff
       this.myContext = context;
}

Intent i = new Intent (myContext, main.class);

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Use activity context instead of this

      Intent i = new Intent (ActivityName.this, main.class);

Where ActivityName is the name of your activity class for example MainActivity

If you are using intent in a non activity context pass the context to the constructor of that class and use the context. The same goes with starting the activity. Use context to start activity in a non activity class context.startActivity(i);

   Context mContext;  
   public ViewBreakout(Context context)
   {
   super(context);
   mContext= context; 
   ....   
   }
   .....
       @Override
    public void onClick(DialogInterface dialog, int which) {
       Intent i = new Intent (mContext, main.class);
       mContext.startActivity(i); 
    }
});

Upvotes: 0

Related Questions