gpt4 book ai didi

Android - 多线程导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 22:07:20 24 4
gpt4 key购买 nike

嗨,每次我的游戏中有东西被破坏后,我都会创建一个新的爆炸动画对象,这是通过在我的表面 View 线程中使用线程类完成的。在短时间内摧毁多艘船只后,应用程序退出 Activity 。这是为什么?

explosion ex = new explosion(x,y);
ex.start();

class explosion extends Thread implements Runnable{

private long startTime;
private int num = 0;
private int x;
private int y;
public explosion(int mx, int my){
x = mx;
y = my;
startTime = System.currentTimeMillis();
}
public void run(){
while(num < 3){
long millis = System.currentTimeMillis();
canvas.drawBitmap(explosions.get(num),x,y,null);
if(millis - startTime > 50){
startTime = System.currentTimeMillis();
num++;
Log.d("Explosion","Animate");
}
}
Log.d("Explosion","End Thread");
}
};

最佳答案

从 developer.android 复制...

Regardless of whether or not you use AsyncTask, always remember these two rules about the single thread model:

-Do not block the UI thread,

-and Make sure that you access the AndroidUI toolkit only on the UI thread.

我相信这条线会让你崩溃......

canvas.drawBitmap(explosions.get(num),x,y,null);

此链接详细介绍了在另一个线程上更新 UI 的步骤:

http://developer.android.com/resources/articles/painless-threading.html

关于Android - 多线程导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506657/

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