06-06 23:50:28.340 27670-27706/mys.timer E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: mys.timer, PID: 27660
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我的代码:
protected void onCreate(Bundle savedInstanceState) {
public void run() {
new Timer().scheduleAtFixedRate(new TimerTask(){
@Override
public void run(){
status.setText(getResources().getString(R.string.timercount));
status.setTextColor(Color.parseColor("#990000"));
}
}
}
请告诉我如何避免我的应用程序自动关闭
发布于 2018-06-07 05:07:59
Android的UI组件不是线程安全的,因此当从异步数据库查询或web服务调用返回时,可能需要从辅助线程更新视图或其他UI组件。如果您从辅助线程运行代码,您可能会看到代码几乎在每次尝试时都会崩溃。
runOnUIThread活动的方法
yourActivity.runOnUiThread(new Runnable(){
public void run() {
// UI code goes here
}
});
我希望这对你有帮助。
https://stackoverflow.com/questions/50729471
复制相似问题