2012-04-10 26 views
0

我使用这个Asnc类我的应用程序,但有时我退出应用程序。它崩溃。 “窗口泄漏错误:在行progDialog.show();” - 猜测ProgressDialog导致问题,因为它仍然引用活动(上下文),但我不能使用getApplicationContext(),如果我使用getApplicationContext(),那么ProgressDialog不会工作。我该如何解决这个问题?Android:异步任务,如何有效处理?

protected void executeSLPWebserviceTask(double latitude, double longitude, 
     String progressStr) { 
    WebserviceTask task = new WebserviceTask(this, progressStr); 
    task.execute(latitude, longitude); 
} 



class WebserviceTask extends AsyncTask<Double, Void, Float> implements OnDismissListener{ 

    Context context; 
    ProgressDialog progDialog; 
    String progressString; 

    public WebserviceTask(Context context, String progressStr) { 

     this.context = context; 
     this.progressString = progressStr; 
     initProgDialog(); 
    } 

    void initProgDialog(){ 
     if(!isCancelled()){ 
      try { 
       progDialog = new ProgressDialog(context); 
       progDialog.setCanceledOnTouchOutside(false); 
       progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.btn_cancel),new DialogInterface.OnClickListener() { 
        public void onClick(final DialogInterface dialog, final int id) { 
         progDialog.dismiss(); 
        } 
       }); 
       progDialog.setMessage(progressString); 
       progDialog.setOnDismissListener(this); 
       progDialog.show(); 
      } catch (Exception e) { 
       Log.e("Web&RemoteServiceActivity", "Failed to add window for web service task"); 
      } 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
     addTask(this); 
     super.onPreExecute(); 
    } 

    protected Float doInBackground(Double... latLong) { 
     Float slpFloat = 0f; 
     if(!isCancelled()){ 
      if(internetServiceBound) 
      { 
       try { 
        slpFloat = internetSLPService.getSLPFromInternet(latLong[0].floatValue(),latLong[1].floatValue()); 
       } catch (RemoteException e) { 
        Log.e("Webservice task", "Failed to get slp - Remote exception"); 
        this.cancel(true); 
       } 
       catch (NullPointerException e) { 
        Log.e("Webservice task", "Failed to get slp - Null pointer exception"); 
        this.cancel(true); 
       } 
      } 
     } 
     return slpFloat; 
    } 

    protected void onPostExecute(Float slpFloat) { 
     if (!isCancelled()) { 
      if(slpFloat >= 300 && slpFloat<=1100){ 
       //seaLevelPressure = slpFloat; 
       setReferenceSeaLevelPressure(slpFloat); 
       updateSeaLevelPressureFromWeb(slpFloat); 
       Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.msg_slp_updated_from_internet) + " : " + slpFloat, Toast.LENGTH_LONG); 
       toast.show(); 
       addToastJob(toast); 
       progDialog.dismiss(); 
       //lastCalibratedTime = System.currentTimeMillis(); 

      }else if(slpFloat==0){ 
       //If SLP value returned is 0, notify slp fetch fail and cancel progress dialog 
       Toast toast = Toast.makeText(getApplicationContext(),getString(R.string.msg_slp_fetch_fail), Toast.LENGTH_SHORT); 
       toast.show(); 
       addToastJob(toast); 
       progDialog.dismiss(); 
      }else{ 
       //Notify invalid SLP and cancel progress dialog 
       Toast toast = Toast.makeText(getApplicationContext(),getString(R.string.msg_internet_slp_invalid), Toast.LENGTH_SHORT); 
       toast.show(); 
       addToastJob(toast); 
       progDialog.dismiss(); 
      } 
     } 
     this.cancel(true); 
    } 

    public void onDismiss(DialogInterface dialog) { 
     this.cancel(true); 
    } 

    @Override 
    protected void onCancelled() { 
     if(progDialog != null){ 
      if(progDialog.isShowing()) 
       progDialog.dismiss(); 

     } 
     super.onCancelled(); 
    } 

} 

回答

2

通过在活动中重写onCreateDialog(int id)来创建progressDialog。在onPreExecute()方法的任务中,使用showDialog(PROGRESS_DIALOG_ID);并在onPostExecute()方法中使用dismissDialog(PROGRESS_DIALOG_ID);

+0

看起来像这应该工作.. :)让我试试....然后我会批准这个答案。我是在ProgressDialog不需要OnCreateDialog()的情况下进行的 – 2012-04-11 09:38:26