Looper.prepare() 与 AlertDialog

发布于 2025-01-01 22:12:27 字数 1324 浏览 1 评论 0原文

我想在游戏中插入一个计时器。如果时间为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2025-01-08 22:12:27

使用 Handler 或 Use 执行此操作

this.runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub

        }
    });

错误本身就说明了整个故事。
如果您不在 Activity/View 的 Parent 类中,则使用一些回调 Mechnasim。
这将帮助您解决问题。
干杯。

Do this using Handler or Use

this.runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub

        }
    });

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.

小伙你站住 2025-01-08 22:12:27

根据您收到的错误判断,您正在将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文