使用 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