我在我的 onCreate()
方法中添加了这部分代码,它使我的应用程序崩溃。
需要帮助。
LOGCAT:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread
that created a view hierarchy can touch its views.
代码:
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 UI 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);
关于android - 应用程序崩溃 "Called From Wrong Thread Exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062818/