应用程序崩溃与"调用从错误的线程异常" [英] app crashing with "Called From Wrong Thread Exception"
本文介绍了应用程序崩溃与"调用从错误的线程异常"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我加入这部分的code在我的的onCreate()
方法,它崩溃我的应用程序。
需要帮助。
logcat的:
android.view.ViewRoot $ CalledFromWrongThreadException:只有原来的线程
创建一个视图层次可以触摸自己的看法。
code:
最后的TextView timerDisplayPanel =(TextView中)findViewById(R.id.textView2);
计时器t =新的Timer();
t.schedule(新的TimerTask(){
公共无效的run(){
timerInt ++;
Log.d(计时器,计时器);
timerDisplayPanel.setText(时间=+ timerInt +秒);
}
},10,1000);
解决方案
只有创建视图层次可以触摸其观点原来的线程。
您正试图改变在非UI线程的UI元素的文本,所以它给exception.Use runOnUiThread
t.schedule(新的TimerTask(){
公共无效的run(){
timerInt ++;
Log.d(计时器,计时器);
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
timerDisplayPanel.setText(时间=+ timerInt +秒);
}
});
}
},10,1000);
I added this part of the code in my onCreate()
method and it crashes my app.
need help.
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.
You are trying to change the text of the UI element in Non UI Thread, so it gives exception.Use runOnUiThread
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);
这篇关于应用程序崩溃与"调用从错误的线程异常"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文