jee*_*wat 12 android handler timertask
我在我的onCreate()方法中添加了这部分代码,它崩溃了我的应用程序.需要帮忙.
logcat的:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread
that created a view hierarchy can touch its views.
Run Code Online (Sandbox Code Playgroud)
码:
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);
Run Code Online (Sandbox Code Playgroud)
Sam*_*iya 33
Only the UI thread that created a view hierarchy can touch its views.
Run Code Online (Sandbox Code Playgroud)
您正在尝试更改非UI线程中的UI元素的文本,因此它给出了exception.Use 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10988 次 |
| 最近记录: |