更新文本视图中的计时器问题

时间:2011-04-05 19:09:47

标签: android

我正在使用一个计时器和文本视图,在计时器运行方法中我不断得到时间,但是当我试图用时间更新文本视图时,它的致命异常,任何人都可以帮助我,我也在发送源代码代码

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clock);
        timeview=(TextView) findViewById(R.id.currenttime);
        Timer timer=new Timer();
        timer.schedule(new UpdateTimeTask(), 100, 100);

    }

    class UpdateTimeTask extends TimerTask{

        @Override
        public void run() {
            // TODO Auto-generated method stub
             int hour= new java.sql.Time(System.currentTimeMillis()).getHours();
               int min=new java.sql.Time(System.currentTimeMillis()).getMinutes();
               int sec=new java.sql.Time(System.currentTimeMillis()).getSeconds();

                String time="TIME is "+hour+":"+min+":"+sec;
                System.out.println(time);
                //ClockActivity.currentTime=time;
            timeview.setText(time);
        }

和logcat如下

04-06 00:03:19.337: WARN/dalvikvm(391): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
04-06 00:03:19.436: ERROR/AndroidRuntime(391): FATAL EXCEPTION: Timer-0
04-06 00:03:19.436: ERROR/AndroidRuntime(391): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.view.View.requestLayout(View.java:8125)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.widget.TextView.checkForRelayout(TextView.java:5378)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.widget.TextView.setText(TextView.java:2688)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.widget.TextView.setText(TextView.java:2556)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at android.widget.TextView.setText(TextView.java:2531)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at com.test.ClockActivity$UpdateTimeTask.run(ClockActivity.java:41)
04-06 00:03:19.436: ERROR/AndroidRuntime(391):     at java.util.Timer$TimerImpl.run(Timer.java:289)
04-06 00:03:19.506: WARN/ActivityManager(67):   Force finishing activity com.test/.ClockActivity
04-06 00:03:19.546: WARN/ActivityManager(67):   Force finishing activity com.test/.HomeActivity
04-06 00:03:20.876: INFO/ActivityManager(67): Displayed activity com.test/.ClockActivity: 2459 ms (total 2459 ms)

例外是fatal exception timer 0

1 个答案:

答案 0 :(得分:6)

每当您想要从另一个不是UI线程的线程修改UI时,请使用runOnUiThread方法:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        timeview.setText(time);
    }
});