سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top