7

I want to display a couple of images and add a delay between each image. I did this and have no errors in the code but for some reason the app crashes.

Bitmap bitmap = BitmapFactory.decodeFile(imageIn);
    ImageView myImageView = (ImageView)findViewById(R.id.imageview);
    myImageView.setImageBitmap(bitmap);
    // Those are the only 2 lines I used to make my handler 
    Handler handlerTimer = new Handler();
    handlerTimer.postDelayed((Runnable) this, 20000);
Gary
  • 13,303
  • 18
  • 49
  • 71
moe
  • 247
  • 3
  • 8
  • 21

1 Answers1

34

You don't say what class hosts the snippet you posted, but I think handlerTimer.postDelayed((Runnable) this, 20000); is unlikely to be right.

Try adding an anonymous Runnable object such as

    handlerTimer.postDelayed(new Runnable(){
        public void run() {
          // do something             
      }}, 20000);

Another thing, logcat output is invaluable for getting clues about what is causing a crash. http://developer.android.com/guide/developing/tools/logcat.html

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • if his "this" whatever it is, implements Runnable, there's no problem doing it like he does. – Vlad Jun 17 '12 at 16:34
  • In addition : to prevent the call to Looper.prepare, you can create the handleTimer this way : handleTimer = new Handler(Looper.getMainLooper()); – Tobliug Mar 18 '15 at 02:26