倒计时器,在UI上显示AsyncTask

时间:2012-05-02 11:14:52

标签: android android-asynctask countdown

我正在尝试实现在UI上显示的倒数计时器, 我认为这是一个干净而优雅的解决方案,但Logcat抛出

AndroidRuntime(3282): java.lang.RuntimeException: An error occured while executing doInBackground()

private class UpdateCountdownTask extends AsyncTask<Long, Long, Void> {
    @Override
    protected Void doInBackground(Long... millisUntilFinished) {

        new CountDownTimer(millisUntilFinished[0], 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                publishProgress(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                ((TextView) findViewById(R.id.answer)).setText(answer);
            }
        };
        return null;
    }

    @Override
    protected void onProgressUpdate(Long... millisUntilFinished) {
        super.onProgressUpdate(millisUntilFinished);
        ((TextView) findViewById(R.id.timer)).setText(""
                + millisUntilFinished[0] / 1000);
    }
}

仍然没有完全理解线程。谁能告诉我我做错了什么?

感谢。

更新:非常抱歉。我绝对不称职。我忘了在柜台上打电话给metod start()。 :P  我终于用CountDownTimer来实现倒计时了。 Sill不知道这是否真的在一个单独的线程上运行,但它现在可以正常工作。

    CountDownTimer myCounter = new CountDownTimer(millisUntilFinished, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            ((TextView) findViewById(R.id.timer)).setText(""
                    + millisUntilFinished / 1000);
            System.out.println("I am ticking");
        }

        @Override
        public void onFinish() {
        }
    };

    myCounter.start();

3 个答案:

答案 0 :(得分:3)

我认为问题是CountDownTimer中的onFinish()方法。在这里你要修改UI线程,你不能从后台线程做到这一点。所以你必须将((TextView) findViewById(R.id.answer)).setText(answer);放在onPostExecute()方法中。我的意思是:

protected void onPostExecute(String result) {
super.onPostExecute(result);
((TextView) findViewById(R.id.answer)).setText(answer);
}

我希望这有效。

答案 1 :(得分:0)

当你在后台做某事时,你将无法连接用户界面。在我看来,如果你使用一个线程,你可能会想要什么。这将使你的工作变得更加容易。下面显示了一小段代码。我希望这可以帮助你。我正在尝试设置图像而不是你可以设置文本。我已经完成了使用线程和处理程序。它无法连接线程内的UI。在assynctask当你在后台做某事时你完全在线程内。这就是为什么你在这里得到一个错误。其他你可以在Postexecute()

中做到这一点
Thread animator = new Thread() {
        public void run() {
            int i = 0;
            try {
                sleep(2000);
                while (i < 4) {
                    sleep(50);
                    handler.sendMessage(handler.obtainMessage(i));
                    i++;
                }
            } catch (Exception e) {

            }
        }
    };
    animator.start();
    handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                animatedimage.setImageResource(R.drawable.sub1);
            } else if (msg.what == 1) {
                animatedimage.setImageResource(R.drawable.sub2);
            } else if (msg.what == 2) {
                animatedimage.setImageResource(R.drawable.sub3);
            } else if (msg.what == 3) {
                animatedimage.setImageResource(R.drawable.sub4);
            }
        }

    };

答案 2 :(得分:0)

您是否考虑过每秒使用Handler更新UI?

结帐http://developer.android.com/resources/articles/timed-ui-updates.html

相关问题