【问题标题】:My app frequently throws android.view.WindowLeaked exception --我的应用经常抛出 android.view.WindowLeaked 异常——
【发布时间】:2010-09-22 19:57:25
【问题描述】:

我的应用经常抛出如下异常:

E/WindowManager(6282): android.view.WindowLeaked: 活动 com.myActivity 已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@4479b710 那 最初是在这里添加的

当主 Activity 启动和启动任务时,应用会显示一个进度对话框。任务完成后,它将关闭进度对话框。

我的代码如下所示。有人可以帮我吗?

public class MyActivity extends Activity {

private static int ID_DIALOG_PROGRESS = 2001;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.my_activity);
    showDialog(ID_DIALOG_PROGRESS);
    new MyTask().execute(null, null, null);
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == ID_DIALOG_PROGRESS) {
        ProgressDialog loadingDialog = new ProgressDialog(this);
        loadingDialog.setTitle("");
        loadingDialog.setMessage("");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(false);
        return loadingDialog;
    }

    return super.onCreateDialog(id);
}

private class MyTask extends AsyncTask<Void, Void, Void> {

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

            /* Do something expensive here...*/

            /* Start other activity*/
            Intent intent = new Intent(MyActivity.this, OtherActivity.class);
            startActivityForResult(intent, 1000);
        }

        return null;
    }

    protected void onPostExecute(Void arg0) {
        dismissDialog(ID_DIALOG_PROGRESS);
    }
}
}

大多数时候,异常是从 showDialog() 调用中引发的。另一次,dismissDialog() 调用引发了异常。

提前谢谢你!

【问题讨论】:

    标签: android


    【解决方案1】:

    在关闭onPostExecute() 中的对话框之前,您正在doInBackground() 中开始一个新活动,这可能是导致对话框泄漏的原因。我会搬家

    Intent intent = new Intent(MyActivity.this, OtherActivity.class);
    startActivityForResult(intent, 1000);
    

    dismissDialog() 通话后转至onPostExecute(),看看会发生什么。

    【讨论】:

      【解决方案2】:

      试一试也是个好习惯……赶上来

      dismissDialog(ID_DIALOG_PROGRESS);
      

      否则,在某些情况下对话框不再可用时,您可能会收到随机应用程序崩溃,例如屏幕旋转后。

      【讨论】:

        猜你喜欢
        • 2014-08-24
        • 2015-12-04
        • 1970-01-01
        • 2013-05-20
        • 2013-05-24
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        相关资源
        最近更新 更多