我正在使用这个Asnc类我的应用程序,但有时我退出应用程序。它崩溃了。 “窗口泄漏错误:在行progDialog.show();” - 猜猜ProgressDialog引起了问题,因为它仍然引用了活动(上下文),但我不能使用getApplicationContext(),如果我使用getApplicationContext(),那么ProgressDialog将不起作用。我该如何解决这个问题?
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();
}
}
答案 0 :(得分:2)
通过覆盖活动中的onCreateDialog(int id)来创建progressDialog。在onPreExecute()方法的任务中使用showDialog(PROGRESS_DIALOG_ID);并在onPostExecute()方法中使用dismissDialog(PROGRESS_DIALOG_ID);