1. 程式人生 > >android.view.WindowManager$BadTokenException: Unable to add window -- token

android.view.WindowManager$BadTokenException: Unable to add window -- token 阿新 發佈:2018-12-12

android.view.WindowManager$BadTokenException: Unable to add window – token [email protected] is not valid; is your activity running? 之所以出現這個問題是因為在activity介面加載出來之前我這手速太快點選彈出了一個dialog,然後就出錯了,找了很多的方法都不適用。網上大多的說法是判斷一下activity是否finished。但是這份在我這個情況下不太適合。然後看到了一個大兄弟的做法,是做一個不依賴activity的系統性的dialog,我覺得很棒 這是原博主的地址

https://www.cnblogs.com/diyishijian/p/5912838.html

然後我這借鑑上面博主的做法,對window設定一個type,然後問題完美解決

  final Dialog dialog = new Dialog(mC, R.style.SquareEntranceDialogStyle);
        Window window = dialog.getWindow();
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        if (window != null) {
            window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            window.setGravity(Gravity.BOTTOM);
        }
        View layout = View.inflate(mC, R.layout.activity_fast_create, null);
        dialog.setContentView(layout);
        dialog.show();
//之後的該做點選做點選該做動畫做動畫