使用 PopupWindow 以前, 必定要知道 Android 是怎麼響應旋轉屏幕的. 若是不知道的話, 那就先去了解下這個知識點. >>> Google 關鍵詞: Android Activity 重建 html
另外須要知道的就是PopupWindow的使用方法. >>> Google 關鍵詞: Android PopupWindow android
若是你直接調用PopupWindow.show***(...)的方法, 那麼系統會回饋你一個錯誤, 以下: post
Unable to add window -- token null is not valid; is your activity running?
若是切實須要在 Activity.onCreate(Bundle) 中彈出 PopupWindow, 那麼你能夠經過 View.post(Runnable) 來完成, ---- 這個調用能夠確保彈出窗口時, Activity 已經完成初始化. google
p.s. View.post(Runnable) 要比 Handler.post(Runnable) 強大, 具體參考Android SDK Source. spa
若是你忘記了, 那麼系統仍是會回饋你一個錯誤, 以下: htm
Activity net.oschina.demo.MainActivity has leaked window android.widget.PopupWindow$PopupViewContainer{42148068 V.E..... ......I. 0,0-297,59} that was originally added here
android.view.WindowLeaked: Activity net.oschina.demo.MainActivity has leaked window android.widget.PopupWindow$PopupViewContainer{42148068 V.E..... ......I. 0,0-297,59} that was originally added here
在這裏就要用到上述轉屏的知識了. 通常狀況, 均可以經過禁止轉屏重建 Activity 來解決這個錯誤. 除此以外, 就是在 onCreate/onResume/onStart/onPause/onStop/onDestroy 等處手動維護 PopupWindow 的狀態, 確保轉屏前, 調用 PopupWindow.dismiss() . blog
p.s. 正是由於這些問題, 因此我不怎麼喜歡用PopupWindow.... token
http://stackoverflow.com/questions/18182168/popup-window-destroy-on-screen-orientation
http://stackoverflow.com/questions/9546061/how-to-keep-popup-window-opened-when-orientation-changes-at-run-time-in-android get
http://www.cnblogs.com/bjzhanghao/archive/2012/11/09/2761897.html
http://wangjianfei1016.blog.163.com/blog/static/202440962013024105751807/ it