android - Show a pop-up dialog at the end of the game on SurfaceView -
i want pop dialog box when user completes game show points , time. read runonuithread
, tried implement solution in code, not working. following error:
"can't create handler inside thread has not called looper.prepare()"
.
i have bool variable called gamecompleted
in surfaceview
class, when becomes true call method showdialoggamecompleted()
implemented in activity
class. wrong?
my activity class:
public class canvastutorial extends activity { final context context = this; dialog dialog; /** called when activity first created. */ @targetapi(build.version_codes.honeycomb_mr2) @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.play_layout); } // method call surfaceview class in order show dialog public void showdialoggamecompleted() { dialog= new dialog(context); dialog.setcontentview(r.layout.custom); dialog.settitle("puzzle completed!"); // set custom dialog components - text, image , button textview text = (textview) dialog.findviewbyid(r.id.text); text.settext("congratulations!\npoints: \nmoves: \ntime: "); imageview image = (imageview) dialog.findviewbyid(r.id.image); image.setimageresource(r.drawable.cup); button dialogbutton = (button) dialog.findviewbyid(r.id.dialogbuttonok); // if button clicked, close custom dialog dialogbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { dialog.dismiss(); } }); canvastutorial.this.runonuithread(new runnable() { public void run() { dialog.show(); } }); }
my surfaceview class:
public class mysurfaceview extends surfaceview implements surfaceholder.callback{ private canvasthread canvasthread; private canvastutorial canvastutorial= new canvastutorial(); public mysurfaceview(context context, attributeset attrs) { super(context, attrs); // todo auto-generated constructor stub getholder().addcallback(this); canvasthread = new canvasthread(getholder(), this); setfocusable(true); } public mysurfaceview(context context) { super(context); getholder().addcallback(this); canvasthread = new canvasthread(getholder(), this); setfocusable(true); } @override public void ondraw(canvas canvas) { if(canvas!=null) { switch (w) { case menu: //menu stuff break; case game: // when game completed call //showdialoggamecompleted method implemented in activity class if(gamecompleted) { gamecompleted=false; canvastutorial.showdialoggamecompleted(); } break; } } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub } @override public void surfacecreated(surfaceholder holder) { canvasthread.setrunning(true); canvasthread.start(); } @override public void surfacedestroyed(surfaceholder holder) { // todo auto-generated method stub boolean retry = true; canvasthread.setrunning(false); while (retry) { try { canvasthread.join(); retry = false; } catch (interruptedexception e) { // try again , again... } } }
my thread class:
public class canvasthread extends thread { private surfaceholder _surfaceholder; private mysurfaceview _mysurfaceview; private boolean _run = false; public canvasthread(surfaceholder surfaceholder, mysurfaceview mysurfaceview) { _surfaceholder = surfaceholder; _mysurfaceview = mysurfaceview; } public void setrunning(boolean run) { _run = run; } @override public void run() { canvas c; while (_run) { c = null; try { c = _surfaceholder.lockcanvas(null); synchronized (_surfaceholder) { _mysurfaceview.ondraw(c); } } { // in if exception thrown // during above, don't leave surface in // inconsistent state if (c != null) _surfaceholder.unlockcanvasandpost(c); } } } }
you can not call thread. because still working. think need use handler. answer. maybe can you. https://stackoverflow.com/a/16886486/3077964
Comments
Post a Comment