如果在 run() 中添加 Show ProgressDialog,Android TimerTask 会抛出 RuntimeException

发布于 2024-10-10 08:55:10 字数 638 浏览 0 评论 0原文

我正在尝试使用计时器任务来安排计时器。我想在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-10-17 08:55:10

发生这种情况可能是因为您尝试在非 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文