首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从IME: BadTokenException创建PopupWindow

从IME: BadTokenException创建PopupWindow
EN

Stack Overflow用户
提问于 2012-12-24 01:46:00
回答 1查看 217关注 0票数 0

我正在尝试从IME服务(屏幕键盘)显示一个没有活动的弹出窗口。当我调用popUp.showAtLocation(layout,Gravity.CENTER,0,-100)时,我得到一个"Windowmanager$BadTokenException: Unable to add window -- token null is is“。我知道从没有关联活动的服务中打开弹出窗口有点不寻常--这可能吗?

下面是我的代码:

代码语言:javascript
复制
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();
    }
}

任何帮助都将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

发布于 2012-12-24 03:00:31

我发现了问题--我试图使用布局绘制弹出窗口,并使用与父级相同的布局。解决方案是将父视图设置为另一个视图。我发现在创建弹出窗口之前,确保视图已经创建(例如,使用处理程序/runnable)也很重要。

代码语言:javascript
复制
public void initiatePopupWindow()
{
    try {
        Log.i("dotdashkeyboard","initiatePopupWindow (from IME service)");
        LayoutInflater inflater = (LayoutInflater) this.getBaseContext().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, false);
        popUp.showAtLocation(inputView, 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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14013184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档