-1

If I use handler.postDelayed with the following order (delaying startAnimation):

animFadeout = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.disappear);
animFadeout.setAnimationListener(this);
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override
    public void run() { 
    txtMessage.startAnimation(animFadeout);
         } 
    }, 4000);

then everything goes smooth. However, if I change the order (delaying loadAnimation):

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override
    public void run() { 
    animFadeout = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.disappear);
         } 
    }, 4000);
    animFadeout.setAnimationListener(this);
    txtMessage.startAnimation(animFadeout);

the application crashes with the message "Unfortunately, my app has stopped working". I am curious about the reason for this result... Thanks

Kfir
  • 157
  • 1
  • 3
  • 11
  • You have to load the animation before you can use it. In the first case you're doing that, you load the animation and then have a handler delay the textview starting the animation. In the second scrap of code though you are having the textview start the animation reference before it has been assigned to the correct animation. – MCWhitaker Aug 14 '13 at 20:03

1 Answers1

0

The problem is with

animFadeout.setAnimationListener(this);

Since you are creating animFadout in the thread (which hasn't run yet), animFadeout is still null. You should be getting a NullPointerException in the LogCat messages.

Have you tried putting all 3 statements in the Runnable?

lifeson106
  • 503
  • 4
  • 11