尝试完成一个输入事件,但是输入事件接收器已经被释放。

25

我为我的列表视图(listview)编写了一个自定义适配器(custom adapter)。该适配器包含一个文本视图和一个图像按钮。我已经在单击图像按钮时实现了弹出菜单(popup menu)。一切都正常工作。但是当从弹出菜单选择选项时,logcat显示一条单行消息“Attempted to finish an input event but input event receiver has already been disposed”,什么也没发生。

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resourceId) {
        super(context, resourceId);
    }

    public MyAdapter(Context context, int resourceId, List<String> string) {
        super(context, resourceId, string);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if(v == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            v = inflater.inflate(R.layout.adapter_layout, null);
        }

        String str = getItem(position);

        if(str != null) {
            TextView textView = (TextView)v.findViewById(R.id.editText1);
            textView.setText(str);
            ImageButton button = (ImageButton)v.findViewById(R.id.imageButton1);
            button.setOnClickListener(new Custom_Adapter_Button_Click_Listener(getItemId(position), getContext()));
        }

        return v;
    }
}

onclicklistener接口是

public class Custom_Adapter_Button_Click_Listener implements OnClickListener, OnMenuItemClickListener {

    long position;
    Context context;

    public Custom_Adapter_Button_Click_Listener(long id, Context appcontext) {
        position = id;
        context = appcontext;
    }



    @Override
    public boolean onMenuItemClick(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        int index = info.position;
        Log.d("ItemClicked", "selected index : " + index);
        switch(item.getItemId()) {
        case R.id.option :
            Toast.makeText(context, "Selected index : " + index, Toast.LENGTH_SHORT).show();
            return true;

        default :
            Toast.makeText(context, "Default", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        PopupMenu popup = new PopupMenu(context, v);
        MenuInflater popupInflater = popup.getMenuInflater();
        popupInflater.inflate(R.menu.contextmenu, popup.getMenu());
        popup.show();   

    }

}
我理解这条消息的意思是,在 onMenuItemClick() 执行之前,某些东西正在捕食事件。我正在 Nexus 5 Android 5.0.1 上运行我的应用程序。
我在这里找到了一个类似问题的解决方案。但是我不知道如何将这种方法用于我的问题。
我尝试使用上下文菜单而不是弹出菜单,但是在单击上下文菜单项后仍然出现相同的消息“尝试完成输入事件,但输入事件接收器已被丢弃”。
请帮助我...!!

3
在以下情况下,我遇到了错误消息: Activity创建了一个对话框(Dialog),在对话框中添加了一个KeyListener。 在按返回按钮后,Activity和Dialog已关闭。显然,在“输入事件”完成之前,对话框就被关闭了。 - A.G.
2个回答

5
我从另一个角度遇到了这个问题:尝试从菜单中启动服务。默认情况下,调试消息不是很相关。我的解决方案是在logcat中消除过滤器,然后我得到另一个消息,即无法首先启动服务(我忘记将其放入清单文件中)。
在你的情况下,你可能需要将toast显示包装成一个类:
public class DisplayToast implements Runnable {
    private final Context mContext;
    private final String mText;

    public DisplayToast(Context mContext, String text) {
        this.mContext = mContext;
        mText = text;
    }

    public void run() {
        Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
    }
}

通过一个Handler对象调用它:

mHandler.post(new DisplayToast(this, "Epic message!"));

或者更好的方法是使用Handler.postDelayed()方法。
希望对你有所帮助。

如何“消除logcat中的过滤器”? - Adam Burley
@Kidburla,在AS中,在“Android Monitor”中,你选择“无过滤器”,而不是“仅显示选定的应用程序”。 - Laur Ivan

0

你必须在super()中传递R.layout.id以防止这个错误。

例如:

public PersonAdapter(Context c, ArrayList<String> list) {
            super(c, R.layout.item_layout, list);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接