gpt4 book ai didi

android - 应用程序崩溃 "Called From Wrong Thread Exception"

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:32:44 26 4
gpt4 key购买 nike

我在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com