timer problem in updating text view
I am using one timer and text view ,in timer run method i am continously getting time ,but when i am trying to updte a text view with time ,its shwing fatal exception ,can anyone help me ,i am also sending source code
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);
}
and the logcat is as follows
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 and开发者_C百科roid.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)
Exception is fatal exception timer 0
.
Whenever you want to modify the UI from another thread which is not the UI Thread, use runOnUiThread
method:
runOnUiThread(new Runnable() {
@Override
public void run() {
timeview.setText(time);
}
});
精彩评论