2

我正在像这样单击按钮时显示 PopupWindow。

public void Search_Click(View view) {
    try 
    {
      Display display=getWindowManager().getDefaultDisplay();
      LayoutInflater inflater = (LayoutInflater) IssueTokenActivity.this
                                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View layout = inflater.inflate(R.layout.activity_pop_up_transporter_details,
                        (ViewGroup) findViewById(R.id.popup_element));
      AutoCompleteTextView act=(AutoCompleteTextView)layout.findViewById(R.id.act_trans_name);
        ArrayAdapter<String> dataadapter=new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_dropdown_item_1line,list);
        dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        act.setAdapter(dataadapter);
        act.setThreshold(1);

        //TODO: Need to support for higher API
        pwindo = new PopupWindow(layout,display.getWidth()-60, display.getHeight()-400, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
        btnClosePopup.setOnClickListener(cancel_button_click_listener);

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

但是在输入 AutoCompleted TextView 时出现此错误。

06-20 09:15:47.091: E/AndroidRuntime(23277): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@40ece840 is not valid; is your activity running?
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
06-20 09:15:47.091: E/AndroidRuntime(23277):    at android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
4

1 回答 1

1

最好使用对话框(android.app.Dialog)来实现AutoCompleteTextView。我认为不可能在PopupWindow中添加AutoCompleteTextView(你会得到异常)。你可以在Popupwindow中添加Spinner。如果使用对话框而不是你可以同时实现弹出。

于 2015-01-20T04:36:09.477 回答