如果在 run() 中添加 Show ProgressDialog,Android TimerTask 会抛出 RuntimeException
我正在尝试使用计时器任务来安排计时器。我想在使用 ProgressDialog 运行任务时冻结 UI。我将 AsyncTask 与 TimerTask 结合使用来达到预期的结果。但是当我将进度对话框代码添加到 TimerTask Runnable 时,它会抛出运行时异常。以下是 TimerTask 的代码,任何帮助将不胜感激。提前致谢。
公共类 MyTimerTask 扩展 TimerTask { 上下文contxt; 公共MyTimerTask(上下文cn){ 上下文=cn;
}
public void run() {
try {
pd=ProgressDialog.show(contxt, "Searching For Records", "Please wait...", true, true);
reqtype="GO";
_getRecords=new InitTask();
_getRecords.execute(contxt);
} catch (Exception e) {
Log.e(">>>>>>>>>>>> Error executing MyAsyncTask: ", e.getMessage(), e);
}
}
}
I am trying to schedule a timer using the timertask. I want to freeze the UI when the task is running using the ProgressDialog. I am using AsyncTask with TimerTask to achieve the desired results. But when I add Progress Dialog code to TimerTask Runnable, it throws Runtime Exception. Below is the code for TimerTask, Any help would be appreciated. Thanks in advance.
public class MyTimerTask extends TimerTask {
Context contxt;
public MyTimerTask(Context cn){
contxt=cn;
}
public void run() {
try {
pd=ProgressDialog.show(contxt, "Searching For Records", "Please wait...", true, true);
reqtype="GO";
_getRecords=new InitTask();
_getRecords.execute(contxt);
} catch (Exception e) {
Log.e(">>>>>>>>>>>> Error executing MyAsyncTask: ", e.getMessage(), e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况可能是因为您尝试在非 GUI 线程中使用 GUI 功能。看看 http://developer .android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29 获取可能的修复。
That probably happens because you are attempting to use GUI features in a nonGUI thread. Have a look at http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29 for a possible fix.