Android:弹出窗口:尝试完成输入事件,但输入事件接收器已被释放

Posted

技术标签:

【中文标题】Android:弹出窗口:尝试完成输入事件,但输入事件接收器已被释放【英文标题】:Android: Popup Window: Attempted to finish an input event but the input event receiver has already been disposed 【发布时间】:2015-03-14 03:36:47 【问题描述】:

我想在用户按下类别按钮时添加一个弹出窗口。该窗口将显示可用类别的列表。弹窗相关代码如下:

onCreate:

    //popup choices
    localArrayList = new ArrayList<String>();
    localArrayList.add("action 1");     //0
    localArrayList.add("action 2");     //1
    localArrayList.add("action 3");     //2
    localArrayList.add("action 4");         //3
    localArrayList.add("action 5");     //4
    popUpContents = new String[localArrayList.size()];
    localArrayList.toArray(popUpContents);
    popupWindowDogs = ppopupWindowDogs();

按下按钮的动作

public void onClickBtn_action(View v) 

    switch (v.getId()) 
    
        case R.id.btn_category:         
            popupWindowDogs.showAsDropDown(v, -5, Constants.SCREEN_H / 15); break;
        default:
            return;
    

弹出窗口:

public PopupWindow ppopupWindowDogs() 
    
        final PopupWindow localPopupWindow = new PopupWindow(this);
        ListView lv = new ListView(this);                                               
        lv.setAdapter(new MyCustomArrayAdapter_POP(DB_Ex_ListActivity.this));
        lv.setOnItemClickListener(new OnItemClickListener() 
        
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
              
                  if (position == 0) 
                    

                     

                    localPopupWindow.dismiss();
              
        );

        localPopupWindow.setFocusable(true);
        localPopupWindow.setOutsideTouchable(true);
        localPopupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        localPopupWindow.setWidth(Constants.SCREEN_W / 2);
        localPopupWindow.setHeight(Constants.SCREEN_H );
        localPopupWindow.setContentView(lv);
        return localPopupWindow;
    

    private class MyCustomArrayAdapter_POP extends BaseAdapter 
    
        private LayoutInflater mInflater;  
        private Context mContext = null;  
        public MyCustomArrayAdapter_POP(Context context) 
          
            mContext = context;  
            mInflater = LayoutInflater.from(mContext);  
          

        public int getCount() 
        
            return localArrayList.size();
        

        public Object getItem(int arg0) 
        
            return localArrayList.get(arg0);
        

        public long getItemId(int position) 
        
            return position;
        

        public View getView(int position, View convertView, ViewGroup parent) 
        
            if (convertView == null) 
            
                convertView = mInflater.inflate(R.layout.listview_item, null);
            
            TextView lv_item_text = (TextView) convertView.findViewById(R.id.lv_item_text);
            lv_item_text.setText(category_list.get(position));
            
            convertView.setLayoutParams(new ListView.LayoutParams((int)(Constants.SCREEN_W/2), (int)(Constants.SCREEN_W/7)));
            return convertView;
        
    

问题:

按下按钮时弹出窗口不显示。 Logcat 报告

Attempted to finish an input event but the input event receiver has already been disposed.

【问题讨论】:

遇到了同样的问题。 [这个答案][1] 对我有用。 [1]:***.com/a/20227508 【参考方案1】:

在调用 showAsDropDown 的下方添加以下代码行

   popupWindowDogs.update(v,200,300 );

根据弹窗的宽度和高度调整200和300。

就是这样!

【讨论】:

【参考方案2】:
lv.setOnItemClickListener(new OnItemClickListener() 
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                  if (position == 0) 
                    

                     else

                          localPopupWindow.dismiss();
                       
              
        );

将你的 localPopupWindow.dismiss() 调用放在 else 中,当 if 条件失败时,它将执行下一行,即 localPopWindow.dimiss()

【讨论】:

以上是关于Android:弹出窗口:尝试完成输入事件,但输入事件接收器已被释放的主要内容,如果未能解决你的问题,请参考以下文章

Vue中 监听移动端软键盘弹出收起事件

当弹出窗口中的 JavaScript 更改输入元素时引发“onChange”事件

Reactjs-我正在尝试处理弹出窗口中的事件,但是在事件执行后弹出窗口没有得到应有的呈现

Android ANR - 输入调度超时

Android : 输入设备键值映射流程

Android:ANR 输入调度超时