开发者

Android TimerTask throws RuntimeException if Show ProgressDialog is added in run()

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 TimerT开发者_如何学编程ask 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜