gpt4 book ai didi

java - Android 中的线程和崩溃

转载 作者:行者123 更新时间:2023-12-02 09:23:03 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com