Android 异步任务上的窗口泄漏
我的Android 异步任务上的窗口泄漏,android,android-asynctask,Android,Android Asynctask,我的AsyncTask中出现了WindowLeaks,我不知道如何处理它,我甚至尝试在onPostExecute上添加一个disclose(),但它仍然会给我带来同样的问题 protected void onPreExecute() { if(this.showLoadingMessage) { this.progressDialog = new ProgressDialog(this.context); this.progressDialog.s
AsyncTask
中出现了WindowLeaks,我不知道如何处理它,我甚至尝试在onPostExecute
上添加一个disclose()
,但它仍然会给我带来同样的问题
protected void onPreExecute() {
if(this.showLoadingMessage) {
this.progressDialog = new ProgressDialog(this.context);
this.progressDialog.setMessage(this.loadingMessage);
this.progressDialog.show();
this.progressDialog.setCancelable(false);
}
super.onPreExecute();
}
protected void onPostExecute(String result) {
if(this.showLoadingMessage && this.progressDialog.isShowing()) {
this.progressDialog.dismiss();
}
result = result.trim();
if(this.asyncResponse != null) {
this.asyncResponse.processFinish(result);
}
if(this.exception != null) {
if(this.exceptionHandler != null) {
this.exceptionHandler.handleException(this.exception);
}
if(this.eachExceptionsHandler != null) {
Log.d(this.LOG, "" + this.exception.getClass().getSimpleName());
if(this.exception instanceof MalformedURLException) {
this.eachExceptionsHandler.handleMalformedURLException((MalformedURLException)this.exception);
} else if(this.exception instanceof ProtocolException) {
this.eachExceptionsHandler.handleProtocolException((ProtocolException)this.exception);
} else if(this.exception instanceof UnsupportedEncodingException) {
this.eachExceptionsHandler.handleUnsupportedEncodingException((UnsupportedEncodingException)this.exception);
} else if(this.exception instanceof IOException) {
this.eachExceptionsHandler.handleIOException((IOException)this.exception);
}
}
}
}
此代码工作时会发生泄漏
HashMap postNotification = new HashMap();
postNotification.put("txtOwner", tvRenter.getText().toString());
AsyncClass taskCount = new AsyncClass(ownerAccept.this, postNotification, new AsyncResponse() {
@Override
public void processFinish(String s) {
if(s.contains("success")){
HashMap postNotif = new HashMap();
postNotif.put("txtOwner", tvRenter.getText().toString());
AsyncClass taskAdd = new AsyncClass(ownerAccept.this, postNotif, new AsyncResponse() {
@Override
public void processFinish(String s) {
if(s.contains("success")){
Log.d(TAG, s);
Intent in = new Intent(ownerAccept.this, RenterTabs.class);
startActivity(in);
finish();
}
}
});
taskAdd.execute("http://carkila.esy.es/carkila/notificationAccept.php");
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
taskCount.execute("http://carkila.esy.es/carkila/notificationCount.php");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_accept.create();
alert.setTitle("Accept");
alert.show();
你什么时候有这些漏洞?请记住,如果启动AsyncTask的活动(或片段)被破坏,则会发生这些泄漏
上下文从哪里来?如果您要结束提供上下文的活动
,您将以泄漏告终。您能发布日志吗?您似乎正在第一个任务的回调中执行新任务,这似乎是导致问题的原因,因为您正在显示一个新的对话框
,而没有关闭旧的对话框,然后完成活动。添加了泄漏的活动,先生。您可以通过将AsyncTasks迁移到IntentServices来避免此问题。AsyncTask依赖于活动或片段生存,如果它们由于内存问题或其他原因而被破坏,您将得到WindowLeaks。我建议您阅读Android的服务指南。要编写的代码更多,但最终还是值得的。+JosephG如果您能显示完整的堆栈跟踪,问题可能还在于您显示对话框的方式。