java - How to start activity on button click in alertDialog -


i have alert dialog setup within class called 'viewbreakout' have button setup fine , creates button when try add intent error message reads "the constructor intent(new dialoginterface.onclicklistener(){}, class) undefined". solution offers remove argument match intent();???

    alertdialog.builder dialogbuilder = new alertdialog.builder(getcontext());     dialogbuilder.settitle("unlucky :(");     dialogbuilder.setmessage("you lost lives");     dialogbuilder.setpositivebutton("try again", new dialoginterface.onclicklistener() {          @override         public void onclick(dialoginterface dialog, int which) {             // todo auto-generated method stub              intent = new intent (this, main.class);//get error here             startactivity(i);         }     });     alertdialog alertdialog = dialogbuilder.create();     alertdialog.show(); 

here code withing class , alertdialog set when global variable class equals 0. stuck , uni project due in soon.

here code class. thought show whole class see if there im missing

    /**     * displays graphical view of 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 draw current state of 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 0 display message      if(lives <= 0){     alertdialog.builder dialogbuilder = new alertdialog.builder(getcontext());     dialogbuilder.settitle("unlucky :(");     dialogbuilder.setmessage("you lost lives");     dialogbuilder.setpositivebutton("try again", new dialoginterface.onclicklistener() {          @override         public void onclick(dialoginterface dialog, int which) {             // todo auto-generated method stub              intent = new intent (this, main.class);             startactivity(i);         }     });     alertdialog alertdialog = dialogbuilder.create();     alertdialog.show();  } 

your intent created inside other class, inner class onclicklistener. "this" refers instance of anonymous inner class onclicklistener.

change :

intent = new intent (myactivity.this, main.class); 

edit :

//add context variable

private context mycontext;  public viewbreakout(context context){         //your stuff        this.mycontext = context; }  intent = new intent (mycontext, main.class); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -