WindowLeaked错误PhoneWindow

时间:2014-05-23 05:59:43

标签: android memory-leaks android-asynctask progressdialog android-windowmanager

我试图在WindowLeakedError ProgressDialog中显示onPreExecute时获得AsyncTask。一些Stackoverflow线程已经提到可能存在Activityfinishing并且我尝试show ProgressDialog在同一Activity中这可能导致此异常的情况但是我已经检查过if activity is in完成状态`,不要显示它。

WindowLeaked Exception

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)

请参阅以下代码:

AsyncTask类

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点击

AsyncTask来电码

FetchDataTask task = new FetchDataTask(MSDSCategories.this);                        
task.execute(TaskType.DOWNLOAD_PDF);

请帮助并指出可能存在的问题。

提前致谢。

此致

1 个答案:

答案 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;
    }