本文介绍了Android的TimerTask的抛出的RuntimeException如果显示ProgressDialog在运行加()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安排使用的TimerTask定时器。我希望在任务是使用ProgressDialog运行冻结UI。我使用的AsyncTask与TimerTask的达到预期的效果。但是,当我添加进度对话框code到TimerTask的Runnable接口,它抛出运行时异常。下面是code为TimerTask的,任何帮助将是AP preciated。先谢谢了。

公共类MyTimerTask扩展的TimerTask {
    上下文contxt;
    公共MyTimerTask(上下文CN){
        contxt = CN;

 }
公共无效的run(){
尝试{PD = ProgressDialog.show(contxt,搜索记录,请稍候...,真实,真实); reqtype =GO;
 _getRecords =新InitTask();
 _getRecords.execute(contxt);}赶上(例外五){
Log.e(>>>>>>>>>>>>错误执行MyAsyncTask:e.getMessage(),E);
}
}
}


解决方案

这可能是因为你正试图使用​​一个线程nonGUI GUI功能。看一看http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29一个可能的修复程序。

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); 
} 
} 
} 
解决方案

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.

这篇关于Android的TimerTask的抛出的RuntimeException如果显示ProgressDialog在运行加()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 06:25