2012-04-10 14 views
0

私はこのAsncクラスを使用していますが、アプリケーションを終了するときがあります。それはクラッシュします。 "window leaked error:行progDialog.show();" - ProgressDialogは、まだアクティビティ(コンテキスト)を参照しているので問題を引き起こしていると推測しますが、getApplicationContext()を使用するとgetApplicationContext()を使用できません。どうすればこの問題を解決できますか?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

これはうまくいくはずです:)私はそれを試してみましょう....そして、私はこの答えを承認します。私は、OnCreateDialog()がProgressDialogに必要ではないという前提のもとで –

関連する問題