我正在尝试从没有活动的 IME 服务(屏幕键盘)中显示一个弹出窗口。当我调用 popUp.showAtLocation(layout, Gravity.CENTER, 0, -100) 时,我收到“Windowmanager$BadTokenException: Unable to add window -- token null is not valid”。我知道从没有相关活动的服务中打开弹出窗口有点不寻常——这可能吗?
这是我的代码:
public void initiatePopupWindow()
{
try {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout,null);
// create a 300px width and 470px height PopupWindow
popUp = new PopupWindow(layout, 300, 470, true);
popUp.showAtLocation(layout, Gravity.CENTER, 0, -100);
Button cancelButton = (Button) layout.findViewById(R.id.popup_cancel_button);
cancelButton.setOnClickListener(inputView.cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
任何帮助,将不胜感激。谢谢