在一个activity的onCreate中写了如下的一段代码:
private final int WATIING_SECS = 5;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (isDialogShowing()) {
dismissWaitDialog();
if(popupwindowQueryState == null
&& ControllerAirPage.this != null
&& rootView != null){
popupwindowQueryState = new GoBackPopupwindow(ControllerAirPage.this,
getString(R.string.time_out),
getString(R.string.air_messge_getting_failed),
true);
popupwindowQueryState.showAtLocation(rootView, Gravity.CENTER, 0, 0);
}
}
}
}, WATIING_SECS * 1000);
大部分时间没有问题,但是当这个popupwindowQueryState没有显示出来,而我直接返回上一页的时候。就会遇到一个报错。
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
解决方案:
应把应把pop.showAtLocation(parent, Gravity.TOP,0, 0)这一句移出oncreate方法,在控件渲染完毕后再使用.