问题:
Unable to add window -- token null is not valid; is your activity running?
使用PopupWindow会遇到的问题,window的展示是需要activity支持的,所以看问题就知道应该是activity还没有渲染完就展示window了,所以报了异常。
解决:
在activity的onWindowFocusChanged()回调里展示window
示例代码:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && mPublishFragment != null){
mPublishFragment.showDraftPopWindow();
}else if (!hasFocus && mPublishFragment != null){
mPublishFragment.dismissDraftPopWindow();
}
}
注意:
onWindowFocusChanged()此方法会多次执行,
showDraftPopWindow()方法内是有展示时机的判断的。
参考地址:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0304/963.html