gpt4 book ai didi

java - 从回调运行计时器时不调用 Looper.prepare()

转载 作者:行者123 更新时间:2023-12-02 00:20:54 24 4
gpt4 key购买 nike

我在这段代码中循环遍历对象的attaylist并为每个对象分配一个回调,但是当我开始使用CountDownTimer时,它崩溃了Can't create handler inside thread that has not call Looper.prepare()

    for ( final ABoxActor a : actList )
{

ActorDamageListener adl = new ActorDamageListener(){
public void ActorDestroyCallback() {
Log.e("KILLED", a.getBitmapName() );
}

public void ActorDamageCallback(float damage) {
Log.e("DAMAGED "+String.valueOf(damage), a.getBitmapName() );
a.setSpriteCurrentFrame(10);

//// THROWS Can't create handler inside thread that has not called Looper.prepare()
CountDownTimer t = new CountDownTimer(500,500){
@Override
public void onFinish() {
a.setSpriteCurrentFrame(15);
}
@Override
public void onTick(long millisUntilFinished) {
}}.start();
/////////////////////////////////

}
};

a.setListener(adl);
}

有什么想法可以最简单地解决这个问题吗?我可以以某种方式将这个“looper”添加到我的回调定义中吗?

谢谢!

最佳答案

您可能在自己的线程中调用此代码。每个 thead 都需要附加一个 Looper,以便能够让处理程序回调到 ui 线程。因此,你必须调用 Looper.prepare() 来设置你的 Looper,以便处理程序可以回调到你的 ui 线程。

关于java - 从回调运行计时器时不调用 Looper.prepare(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10985814/

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