我有两个不起作用的AsyncTask。必须有一些共同点,我做错了。无法停止AsyncTask中的ProgressDialog
我的第一个活动需要很长时间来加载,所以我希望有一个progressDialog/Bar,但它失败。我访问的AsyncTask在我的onCreate与
new HeavyWorker(this).execute();
public class HeavyWorker extends AsyncTask < String , Context , Void > {
private ProgressDialog progressDialog ;
private Context targetCtx ;
public HeavyWorker (Context context) {
this.targetCtx = context ;
// this.needToShow = true;
progressDialog = new ProgressDialog (targetCtx) ;
progressDialog.setCancelable (false) ;
progressDialog.setMessage ("Retrieving data...") ;
progressDialog.setTitle ("Please wait") ;
progressDialog.setIndeterminate (true) ;
}
@ Override
protected void onPreExecute () {
progressDialog.show () ;
}
@ Override
protected Void doInBackground (String ... params) {
// Do Your WORK here
低于做工精细这些线时,在正常
“公共无效的onCreate(捆绑savedInstanceState)”区域
Ls = new LoadSettings(CreateAppointment.this);
gs = new GlobalSubs();
TimeZoneList();
此行动态填充一个微调框并阻止它工作,所以我评论了它。
// createTimezone();
ZoneList(Ls.getCountryZone());
该行还填补了spinnerbox动态地使我评论它
// createZone();
InitialiseUI();
CreateFileName();
return null ;
}
该区域从未达到过?
@ Override
protected void onPostExecute (Void result) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss () ;
}
}
}
我的第二页失败,出现相同风格的问题。
我也尝试在设置页面中使用稍微不同的代码。 与现在修正:
//
private class MyClass extends AsyncTask<Void, Void, Void>{
ProgressDialog mProgressDialog;
public MyClass(){
mProgressDialog = new ProgressDialog(Preferences.this);
mProgressDialog.setMessage ("Please Wait While Saving");
mProgressDialog.setTitle("Saving");
//
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show(Preferences.this, "Saving", "Please Wait a Sec");
}
@Override
protected Void doInBackground(Void... params)
{
saveSettings();
return null;
}
@Override
protected void onPostExecute(Void unused) {
它的工作的唯一办法是,当我注释掉该行并切换到不同的活动
// mProgressDialog.dismiss();
Toast.makeText(Preferences.this, "Your Settings Where Saved.", Toast.LENGTH_SHORT).show();
gotoMainPage();
}
}
private void gotoMainPage(){
Intent a = new Intent(this, Main_entry.class);
finish();
startActivity(a);
}
你能否描述你的问题而不中断发布的代码?你的ASyncTask如何实现几乎是无法理解的。 – WarrenFaith 2012-08-02 01:17:34