Android 开发 Tip 3 -- that was originally added here

来源:互联网 发布:竹笛教学的软件 编辑:程序博客网 时间:2024/06/05 16:57

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/70185952


窗体句柄泄漏

异常信息如下:

xxx has leaked window DecorView@ffd62e7[title] that was originally added here

这里写图片描述

这类错误一般都是在使用AlertDialog , PopupWindow, ProgressDialog 时,关闭activity时没有将它们 dismiss()


还原一下错误现场:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_alert_dialog);        progressDialog = ProgressDialog.show(this, "title", "loading...");        MyHandler handler = new MyHandler(this);        handler.sendEmptyMessageDelayed(1, 3000);    }
private static class MyHandler extends Handler {        private WeakReference<TestAlertDialogActivity> reference;        public MyHandler(TestAlertDialogActivity activity) {            this.reference = new WeakReference<TestAlertDialogActivity>(activity);        }        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if (msg.what == 1 && reference.get() != null && !reference.get().isFinishing()) {                reference.get().finish();            }        }    }

在ProgressDialog还在运行过程中,调用activity finish()方法就会出现这个错误!


解决办法

在OnDestory() 生命周期函数中,关闭该关闭的 “Dialog” !

 @Override    protected void onDestroy() {        super.onDestroy();        if (progressDialog != null && progressDialog.isShowing()) {            progressDialog.dismiss();        }    }

stackoverflow

http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added

1 0