【问题标题】:Leaked windows error with progress dialog android带有进度对话框android的泄漏Windows错误
【发布时间】:2016-07-14 08:38:50
【问题描述】:

我在显示 progress dialogAsyncTask 时遇到问题:

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

            }
        };
    }

}

我搜索了解决方案,但它仍然不起作用

【问题讨论】:

标签: java android android-asynctask progressdialog


【解决方案1】:

一些提示:

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

  2. AsyncTask 中使用WeakReference&lt;Context&gt; 让垃圾收集器完成它的工作;

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

【讨论】:

  • 我试着按照你的建议做,但它不起作用错误是一样的..
  • 会不会是你在前一个任务完成之前启动了一个新的 AsyncTask?
  • 不,我从来没有开始另一个.. 所以我不明白为什么我会遇到这个问题
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    你可以在这里找到它们的区别:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多