解决 Attempting to destroy the window while drawing!

时间:2021-04-29 17:21:00

当对Dialog进行关闭时,如果有大量的操作,比如动画、绘图什么的,就可能出现这样的错误

Attempting to destroy the window while drawing!

比如,我在自定义的Dialog中的dismiss中进行了这样的操作,然后就报错了。其实可以忽略的,但毕竟不爽。

    @Override
public void dismiss() {
Animation anim = AnimationUtils.loadAnimation(context, R.anim.dialog_main_hide_amination); anim.setAnimationListener(new AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationRepeat(Animation animation) {
} @Override
public void onAnimationEnd(Animation animation) { Dialog.super.dismiss(); }
}); Animation backAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_root_hide_amin); view.startAnimation(anim);
backView.startAnimation(backAnim);
}

通过Google查到了解决办法。——通过handler来解决

参考网址:http://*.com/questions/17923577/dialogfragment-animation-of-layout-and-attempting-to-destroy-the-window-while-d

解决 Attempting to destroy the window while drawing!

@Override
public void onAnimationEnd(Animation animation) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dismiss();
}
}, 10);

You don't need the 10 ms delay. You can simply use the post() method.

后来,我在下发评论中发现了post可以直接解决,不用10mms的时间。于是就用了post。下面是最终解决问题后的代码:

    @Override
public void dismiss() {
Animation anim = AnimationUtils.loadAnimation(context, R.anim.dialog_main_hide_amination); anim.setAnimationListener(new AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationRepeat(Animation animation) {
} @Override
public void onAnimationEnd(Animation animation) {
view.post(new Runnable() {
@Override
public void run() {
Dialog.super.dismiss();
}
}); }
});
Animation backAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_root_hide_amin); view.startAnimation(anim);
backView.startAnimation(backAnim);
}