Android游戏制作退出框遇到的问题:object not locked by thread before wait()和Can't create handler inside thread
2013-07-23 15:36
681 查看
目标效果:在生命小于0时,出现游戏结束画面,然后画面停顿1.5s后显示弹出框问是继续还是退出。
![](https://img-blog.csdn.net/20130723152135296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXJpZXNfeno=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20130723152103984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXJpZXNfeno=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
我在制作工程中出现问题有两:
当时为了让画面停顿,即所在线程wait,代码是这么写,直接
然后就报错了,在http://stackoverflow.com最多人支持的回答是这样的:
![](https://img-blog.csdn.net/20130723152546468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXJpZXNfeno=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
所以,代码最后改为:
效果达到,修改成功。
然后停顿过后,为了显示结束框,代码也是直接这么写的:
然后就不出所料地报错了- -,这个问题显而易见,也是比较常犯的错误:更新UI是要主线程来更新,即UI线程更新。
最多支持的回答:
![](https://img-blog.csdn.net/20130723153232625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXJpZXNfeno=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
所以,最后代码就改为:
效果达到,修改成功。
其实,除了用runOnUiThread还可以使用handler,具体可见:http://www.2cto.com/kf/201302/190591.html
谢谢大家观看,欢迎对上述代码提出批评。
全部最终代码:
我在制作工程中出现问题有两:
问题1:java.lang.IllegalMonitorStateException:
object not locked by thread before wait():
当时为了让画面停顿,即所在线程wait,代码是这么写,直接try { Thread.currentThread().wait(700); } catch (InterruptedException e) { e.printStackTrace(); }
然后就报错了,在http://stackoverflow.com最多人支持的回答是这样的:
所以,代码最后改为:
synchronized (Thread.currentThread()) { try { Thread.currentThread().wait(700); } catch (InterruptedException e) { e.printStackTrace(); } }
效果达到,修改成功。
问题2:Can't
create handler inside thread that has not called Looper.prepare():
然后停顿过后,为了显示结束框,代码也是直接这么写的:new AlertDialog.Builder(RaidenView.this.getContext()) .setTitle("游戏结束") .setMessage("你的得分是: " + score + "。") .setPositiveButton("再来一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Intent intent = new Intent("com.example.raiden2.RaidenViewActivity"); // startActivity(intent); // RaidenViewActivity ractivity = (RaidenViewActivity) RaidenView.this.getContext(); handler.sendMessage(new Message()); } }).setNegativeButton("退出游戏", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { RaidenViewActivity.this.finish(); } }).show();
然后就不出所料地报错了- -,这个问题显而易见,也是比较常犯的错误:更新UI是要主线程来更新,即UI线程更新。
最多支持的回答:
所以,最后代码就改为:
RaidenViewActivity.this.runOnUiThread(new Runnable() { @Override public void run() { //对话框显示,如上 }}
效果达到,修改成功。
其实,除了用runOnUiThread还可以使用handler,具体可见:http://www.2cto.com/kf/201302/190591.html
谢谢大家观看,欢迎对上述代码提出批评。
全部最终代码:
void drawEndImg() { if(lives.size()==0) { Rect rect1 = new Rect(0, 0, endImg.getWidth(), endImg.getHeight()); Rect rect2 = new Rect(0, 0, cxf_width, cxf_height); cxf_thread = false; mSensorMgr.unregisterListener(this); canvas.drawBitmap(endImg, rect1, rect2, null); synchronized (Thread.currentThread()) { try { Thread.currentThread().wait(700); } catch (InterruptedException e) { e.printStackTrace(); } } RaidenViewActivity.this.runOnUiThread(new Runnable() { @Override public void run() { synchronized (Thread.currentThread()) { try { Thread.currentThread().wait(1500); } catch (InterruptedException e) { e.printStackTrace(); } } new AlertDialog.Builder(RaidenView.this.getContext()) .setTitle("游戏结束") .setMessage("你的得分是: " + score + "。") .setPositiveButton("再来一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Intent intent = new Intent("com.example.raiden2.RaidenViewActivity"); // startActivity(intent); // RaidenViewActivity ractivity = (RaidenViewActivity) RaidenView.this.getContext(); handler.sendMessage(new Message()); } }).setNegativeButton("退出游戏", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { RaidenViewActivity.this.finish(); } }).show(); } }); } }
相关文章推荐
- android 开发遇到Can't create handler inside thread that has not called Looper.prepare()的问题
- Android—子线程更新UI问题( java.lang.RuntimeException: Can't create handler inside thread that has not cal)
- Android java.lang.RuntimeException: Can\'t create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- 记录一次因为Android版本不同导致的crash异常:Can't create handler inside thread that has not called Looper.prepare()
- Android开发报错 Can't create handler inside thread that has not called Looper.prepare()
- Android : Can't create handler inside thread that has not called Looper.prepare()
- Android中的Can't create handler inside thread that has not called Looper.prepare()异常
- Android java.lang.RuntimeException: Can't create handler inside thread that has not called
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- android -- Can't create handler inside thread that has not called Looper.prepare()
- Android在Activity中启动一个新进程报错:Can't create handler inside thread that has not called Looper.prepare()。
- Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare
- android异常收集-Can't create handler inside thread that has not called Looper.prepare()
- Android开发报错 Can't create handler inside thread that has not called Looper.prepare()
- android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
- Unity3d for android 访问UI控件报错 Can\'t create handler inside thread that has not called Looper.prepare()
- Looper如何和一个线程进行绑定,以及Android中的Can't create handler inside thread that has not called Looper.prepare()
- Android:Can't create handler inside thread that has not called Looper.prepare()