应用程序崩溃与“调用错误线程异常”
问题描述:
我在我的onCreate()
方法中添加了这部分代码,它崩溃了我的应用程序。 需要帮助。应用程序崩溃与“调用错误线程异常”
logcat的:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread
that created a view hierarchy can touch its views.
CODE:
final TextView timerDisplayPanel = (TextView) findViewById(R.id.textView2);
Timer t = new Timer();
t.schedule(new TimerTask(){
public void run(){
timerInt++;
Log.d("timer", "timer");
timerDisplayPanel.setText("Time ="+ timerInt +"Sec");
}
},10, 1000);
答
Only the original thread that created a view hierarchy can touch its views.
您正试图改变UI元素在非UI线程的文本,所以它提供了异常。使用runOnUiThread
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
timerInt++;
Log.d("timer", "timer");
runOnUiThread(new Runnable() {
@Override
public void run() {
timerDisplayPanel.setText("Time =" + timerInt + "Sec");
}
});
}
}, 10, 1000);
(我不知道,如何使用处理程序。) - 我读了一些我需要使用处理程序对象。\ –