泄漏的窗口错误与进度对话框android

时间:2016-07-14 08:38:51

标签: java android android-asynctask progressdialog

我在显示progress dialog AsyncTask时遇到问题:

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

    }
}

这是我的活动:

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();
                }

            }
        };
    }

}

我搜索了解决方案,但无论如何都无法正常工作

4 个答案:

答案 0 :(得分:2)

一些提示:

  1. 在活动中保留对ProgressDialog的引用,并添加公开方法以显示/隐藏它;

  2. WeakReference<Context>中使用AsyncTask让垃圾收集者完成工作;

  3. 摆脱阻止UI线程的myClientTask.get()

答案 1 :(得分:1)

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

答案 2 :(得分:0)

问题是在后台调用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;
    }

答案 3 :(得分:0)

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

你可以在这里发现差异:

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