【问题标题】:Creating PopupWindow from IME: BadTokenException从 IME 创建 PopupWindow:BadTokenException
【发布时间】:2012-12-23 17:46:00
【问题描述】:

我正在尝试从没有活动的 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();
    }
}

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

【问题讨论】:

    标签: keyboard popupwindow ime


    【解决方案1】:

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

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-09
      • 2014-10-01
      • 2019-06-17
      • 2015-03-23
      • 2015-04-06
      • 2013-12-14
      • 1970-01-01
      • 2019-02-20
      • 2016-03-08
      相关资源
      最近更新 更多