android delay using handler

空扰寡人 提交于 2019-12-18 16:46:32

问题


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);

回答1:


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



来源:https://stackoverflow.com/questions/5623578/android-delay-using-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!