我试图在WindowLeakedError
ProgressDialog
中显示onPreExecute
时获得AsyncTask
。一些Stackoverflow线程已经提到可能存在Activity
为finishing
并且我尝试show ProgressDialog
在同一Activity
中这可能导致此异常的情况但是我已经检查过if activity is in
完成状态`,不要显示它。
05-23 10:43:27.528: E/WindowManager(21108): Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108): android.view.WindowLeaked: Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.Window$LocalWindowManager.addView(Window.java:541)
05-23 10:43:27.528: E/WindowManager(21108): at android.app.Dialog.show(Dialog.java:301)
05-23 10:43:27.528: E/WindowManager(21108): at com.triplebottomline.ezmsds.web.FetchDataTask.onPreExecute(FetchDataTask.java:47)
05-23 10:43:27.528: E/WindowManager(21108): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-23 10:43:27.528: E/WindowManager(21108): at android.os.AsyncTask.execute(AsyncTask.java:511)
05-23 10:43:27.528: E/WindowManager(21108): at com.example.MyActivity$4.onClick(MyActivity.java:467)
请参阅以下代码:
public class FetchDataTask extends AsyncTask
{
private Activity callerActivity;
private ProgressDialog dialog;
public FetchDataTask (Activity activity)
{
callerActivity = activity;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(callerActivity);
dialog.setCancelable(false);
if(callerActivity != null && !callerActivity.isFinishing())
{
dialog.show();
}
}
@Override
protected void onPostExecute(Object result) {
dialog.dismiss();
dialog = null;
}
整个Activity
代码太大了。但是我发布的代码触发了AsyncTask
Button
点击
FetchDataTask task = new FetchDataTask(MSDSCategories.this);
task.execute(TaskType.DOWNLOAD_PDF);
请帮助并指出可能存在的问题。
提前致谢。
此致
答案 0 :(得分:2)
你对Activity
有强烈的引用,这使得它不会被垃圾收集。请改用弱引用:
public class FetchDataTask extends AsyncTask
{
private WeakReference<Activity> callerActivity;
private WeakReference<ProgressDialog> dialog;
public FetchDataTask (Activity activity) {
callerActivity = new WeakReference<Activity>( activity );
}
@Override
protected void onPreExecute() {
if( callerActivity.get() != null ) {
dialog = new WeakReference<ProgressDialog>( new ProgressDialog( callerActivity.get() ) );
dialog.get().setCancelable(false);
if( callerActivity.get() != null && !callerActivity.get().isFinishing()) {
dialog.get().show();
}
}
}
@Override
protected void onPostExecute(Object result) {
if( dialog.get() != null )
dialog.get().dismiss();
dialog = null;
}