0

我的代码崩溃了,我需要一些帮助。除了这部分,一切都运行良好。

没有错误,但它在timer.schedule(loadImg2, 5000);那之前崩溃了if (!c.moveToNext())

我的问题:我是否在循环中正确使用了计时器?因为那是代码崩溃的地方。
我从来没有看到这个日志"+-+-+-+-+-+-+-+-+- Getting out "或之后的任何东西。

do
{
    Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Ttrying to cancel ");
    //timer.cancel();
    Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Timer canceled ");

    timer = new Timer();

    Log.v("log_tag", "+-+-+-+-+-+-+-+-+- New timer created ");
    //delay  amount of time(5s here) in milliseconds before first execution.
    //period  amount of time(1s here) in milliseconds between subsequent executions.

    timer.schedule(loadImg2, 5000); //this did not produce any effect so far

    if (!c.moveToNext())
    {
        //destroy
        timer.cancel();
        myImageView.setImageBitmap(null);
        Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Getting out ");
        //get out of the loop or set c.moveToFirst()
        break;
    }
} while (true);

TimerTask loadImg2 = new TimerTask()
{
    @Override
    //Load Img2
    public void run()
    {
        runOnUiThread(new Runnable()
        {
            public void run()
            {
                Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Inside loadImg2 ");
                titleText.setText(DisplayTitle(c));
                Bitmap bitmap2 = BitmapFactory.decodeFile(c.getString(5));
                myImageView = (ImageView) findViewById(R.id.imageview1);
                myImageView.setImageBitmap(null);
                timer.cancel();
            }
        });
    }
}
4

1 回答 1

1

有一个问题是您不能多次设置 timer.schedule()。尝试捕获异常:

try{
  timer.schedule(loadImg2, 5000);
} catch (IllegalArgumentException e){
  Log.v(TAG, "IllegalArgumentException");
} catch (IllegalStateException e){
  Log.v(TAG, "IllegalStateException");
}

然后应用程序不会崩溃。但是计时器将在第一次设置。

我不知道这个问题的解决方案。我也在努力寻找。

于 2013-07-23T15:55:15.000 回答