0

I'm in this code I am cycling through an attaylist of objects and assigning each object a callback, yet when I get to using a CountDownTimer, it crashes with Can't create handler inside thread that has not called 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); 
    }

Any ideas what would be the easiest way to fix that? Can I somehow add this "looper" to my callback definition?

Thanks!

Roger Travis
  • 8,402
  • 18
  • 67
  • 94
  • My answer here should help you: http://stackoverflow.com/questions/10403858/java-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/10404099#10404099 – DeeV Jun 11 '12 at 19:01

1 Answers1

3

You are probably calling this code in your own thread. Each thead needs to have a Looper attached to be able to let a handler call back into the ui thread. Therefore you have to call Looper.prepare() to setup you looper so that the handler can call back to your ui thread.

Moritz
  • 10,124
  • 7
  • 51
  • 61
  • Thanks! :) But... how do I setup this looper? Simply adding "Looper.prepare();" gives "Only one Looper may be created per thread" error. – Roger Travis Jun 11 '12 at 18:59
  • I would expect that the thread calling your actordamangelistener needs to have a looper. – Moritz Jun 11 '12 at 19:17
  • hmm... tried adding this "Looper.prepare();" everywhere within the thread, still throws "Only one Looper may be created per thread" :) – Roger Travis Jun 11 '12 at 19:23