Looper.prepare() 与 AlertDialog
我想在游戏中插入一个计时器。如果时间为 0,则会出现一个 AlertDialog,告诉用户时间已到,并返回到上一个 Activity。下面是方法(它位于扩展 SurfaceView 的类中):
public void showTime(){
time--;
Log.i("GameView time", "" + time);
if (time <= 0){
Log.i("gameview time","time out");
gameTimer.setRunning(false);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Time is out. You lose.");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
main.onBackPressed();
}});
alert.show();
}
}
GameTimer 类是一个线程:
public class GameTimer extends Thread{
private GameView gameView;
private boolean run;
public GameTimer(GameView gameView){
this.gameView = gameView;
}
public void setRunning(boolean value){
this.run = value;
}
public void run(){
Looper.prepare();
while (run){
try {
gameView.showTime();
sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
}
Looper.loop();
}
}
AlertDialog 出现,但应用程序崩溃,并显示消息:只有创建视图层次结构的原始线程才能触摸视图。但这是创建的线程...问题出在哪里?
I would like to insert a time counter inside a game. If the time is 0, there would be an AlertDialog which tells the user the time is out, and goes back to the previous Activity. Here is the method (it is inside a class which extends a SurfaceView):
public void showTime(){
time--;
Log.i("GameView time", "" + time);
if (time <= 0){
Log.i("gameview time","time out");
gameTimer.setRunning(false);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Time is out. You lose.");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
main.onBackPressed();
}});
alert.show();
}
}
The GameTimer class is a Thread:
public class GameTimer extends Thread{
private GameView gameView;
private boolean run;
public GameTimer(GameView gameView){
this.gameView = gameView;
}
public void setRunning(boolean value){
this.run = value;
}
public void run(){
Looper.prepare();
while (run){
try {
gameView.showTime();
sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
}
Looper.loop();
}
}
The AlertDialog appears, but the app crashes, with the message: Only the original thread that created a view hierarchy can touch views. But this is the thread which created... Where's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Handler 或 Use 执行此操作
错误本身就说明了整个故事。
如果您不在 Activity/View 的 Parent 类中,则使用一些回调 Mechnasim。
这将帮助您解决问题。
干杯。
Do this using Handler or Use
The error itself tells the whole story.
And if you are not in Activity/ View's Parent class then use some callback Mechnasim.
This will help you to resolve your issue.
Cheers.
根据您收到的错误判断,您正在将 View 对象从其他地方传递到 Thread 类构造函数中,然后尝试使用它的方法创建 AlertDialog。不幸的是,这行不通。
您需要使用 Handler 将消息(在您的情况下,时间= 0)从您的线程发送回您的 View 类,您在其中定义了 showTime() 方法。
定义 Handler,然后重写 handleMesage() 方法来调用 showTime() 方法。
下面的链接可以帮助您开始。
http://developer.android.com/reference/android/os/Handler.html
Judging by the error you are getting, you are passing your View object into the Thread class constructor from somewhere else and then trying to use it's method to create an AlertDialog. Unfortunately this will not work.
You need to use Handler to send back the message(in your case, time = 0) from your thread to your View class , where you have defined the showTime() method.
Define the Handler and then override the handleMesage() method to call your showTime() methid.
Below link can help you get started.
http://developer.android.com/reference/android/os/Handler.html