2011-04-11 60 views
7

我想显示几个图像并在每个图像之间添加延迟。 我这样做,并没有在代码中的错误,但由于某种原因,应用程序崩溃。android延迟使用处理程序

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

回答

34

你不说什么类主机您发布的片断,但我认为handlerTimer.postDelayed((Runnable) this, 20000);不太可能是正确的。

尝试增加一个匿名Runnable对象,如

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

另一件事,logcat输出是非常宝贵的用于获取关于什么是导致飞机坠毁的线索。 http://developer.android.com/guide/developing/tools/logcat.html

+0

如果他的“this”不管它是什么,实现Runnable,那么就像他那样做没有问题。 – Vlad 2012-06-17 16:34:28

+0

另外:为了防止调用Looper.prepare,你可以这样创建handleTimer: handleTimer = new Handler(Looper.getMainLooper()); – Tobliug 2015-03-18 02:26:24