java - Android 中的线程和崩溃

标签 java android multithreading crash runnable

我会在我的类中调用一个简单的线程,但是当我调用它时,应用程序崩溃了。这是主题:

private void startGame() {
    new Thread(new Runnable() {
        public void run() {
            while (isGiocoAttivo()) {
                try {
                    Thread.sleep(velocitaDiGioco);
                    accendiBomba();
                } catch (InterruptedException ex) {
                }
            }

        }
    }).start();
}

如何解决? AccendiBomba 方法:

private void accendiBomba() {
    try {
        do {
            this.x = (int) Math.round(Math.random() * (righe - 1));
            this.y = (int) Math.round(Math.random() * (colonne - 1));
        } while (!this.action(this.x, this.y));
    } catch (CampoException ex) {
    }
}

最佳答案

在您的线程中执行以下操作

private void startGame() {
new Thread(new Runnable() {
    public void run() {
        while (isGiocoAttivo()) {
            try {
                Thread.sleep(velocitaDiGioco);
               runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    accendiBomba() 
                }
            });
            } catch (InterruptedException ex) {
            }
        }

    }
}).start();}

关于java - Android 中的线程和崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17428047/

相关文章:

java.io.IOException : No such file or directory

Android:媒体播放器警告 (1,44)

java - 旋转 180 度 android 无法正常工作

c - 当内存被一个线程修改并被其他线程读取时,使用 pthread 和互斥锁保护内存的最佳方法是什么?

java - 如何以编程方式在 ActionBar 工具栏中创建过滤器图标

java - 如何将字符串ArrayList转换为char数组

java - org.junit.contrib.java.lang.system.StandardOutputStreamLog 在哪里?

android - 缩放中央图像回收器 View

android - WebView 线程永不停止(WebViewCoreThread、CookieSyncManager、http[0-3])

php - 如何使我的文件上传后端脚本(PHP、MySQL)线程安全?