进度对话框泄漏 windows 错误 android

Leaked windows error with progress dialog android

我在显示带有 AsyncTaskprogress dialog 时遇到问题:

Activity has leaked window com.android.internal.policy.PhoneWindow$DecorView{8cee959 V.E...... R......D 0,0-1026,252} that was originally added here

这是我的异步任务:

public class MyClientTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog = null;

    private Context mContext;

    MyClientTask(Context mContext) {
        this.mContext = mContext;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("Working ...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            wait(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }

    }
}

这是我的 activity:

public class ConnectionActivity extends Activity {

    final private Context mContext = this;
    private Button buttonConnect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R .layout.activity_socketconnection);

        buttonConnect = (Button) findViewById(R.id.buttonConnect);

        buttonConnect.setOnClickListener(getConnectOncOnClickListener());
    }

    private OnClickListener getConnectOncOnClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyClientTask myClientTask = new MyClientTask(mContext);
                try {

                        myClientTask.execute();
                        myClientTask.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

            }
        };
    }

}

我搜索了解决方案,但无论如何都行不通

这通常发生在您完成 activity 而不先调用 dismiss() 或隐藏对话框时。我建议您稍微修改一下代码,在 Activity 中创建实际的 ProgressDialog,然后存储对它的引用。在你的 onDestroy() 中,作为预防措施检查它是否不为 null 并显示,然后 dismiss() 它。

一些提示:

  1. 在 activity 中保留对 ProgressDialog 的引用,并向 show/hide 添加 public 方法;

  2. 在您的 AsyncTask 中使用 WeakReference<Context> 让垃圾收集器完成它的工作;

  3. 去掉阻塞UI线程的myClientTask.get()

问题是在后台调用 wait()。请看here.

样本:

 private Object lock = new Object();

    @Override
    protected Void doInBackground(Void... arg0) {

        synchronized(lock) {
            try {
                lock.wait(5000);
                //  Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

使用progressDialog.cancel();而不是 progressDialog.dismiss();

您可以在此处找到 b/w 它们的区别:

What is the difference between a dialog being dismissed or canceled in Android?