我创建了一个每1秒调用一次的函数,并在我编写的函数中
myButton.setText("Stop");
现在一切都运行良好没有该代码,但每当我编写此代码时,我在我的测试上显示消息“app已停止工作” 我一直在寻找解决方案3天,帮助将不胜感激!
这是我的代码
check_time = new TimerTask() {
@Override
public void run() {
myButton.setText("Stop");
Calendar lo_Calender = Calendar.getInstance();
int current_h = lo_Calender.get(Calendar.HOUR);
int current_m = lo_Calender.get(Calendar.MINUTE);
if (H_toWake == current_h && M_toWake == current_m) {
out.println("ring");
alarm();
//return;
} else {
out.println("no ring!");
}
}
};
time.schedule(check_time, 0, 1000);
}
答案 0 :(得分:4)
您无法从后台线程触摸UI。使用AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:1)
首先,确切的错误应该在你的logcat中。它会告诉你出了什么问题。首先,看看你自己。
在这种特定情况下,TimerTasks作用于一个单独的线程。您只能更改UI线程上的UI。将该部分放在runOnUiThread块中,你会没事的。