1

我正在尝试实现在 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);
    }
}

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

谢谢。

更新:非常抱歉。我是一个绝对无能的人。我忘记在柜台上调用方法 start()。:PI 终于用 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();
4

3 回答 3

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

我希望这行得通。

于 2012-05-02T11:24:48.567 回答
0

在后台执行某些操作时,您将无法连接 UI。我认为,如果您使用线程,您想要的一切都是可能的。这将使您的工作更加轻松。下面显示了一小段代码。我希望这个可能对你有帮助。我正在尝试设置图像而不是你可以设置文本。这里我已经使用线程和处理程序完成了。在线程内连接 UI 是不可能的。在 assynctask 当你在后台做某事时,你完全在里面线程。这就是为什么你在这里得到一个错误。Otherwinse你可以做到这一点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);
            }
        }

    };
于 2012-05-02T11:25:27.323 回答
0

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

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

于 2012-05-02T11:27:48.883 回答