Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android - Attempted to finish an input event but input event receiver has already been disposed

I have a custom adapter for my listview. The adapter contains a textview and a image button. I have implemented a popup menu on clicking the image button. Everything is working fine. But when selecting the options from popup menu, logcat displaying a single line message "Attempted to finish an input event but input event receiver has already been disposed" and nothing is happening.

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 interface is

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

    }

}

What I understood from the message is that some thing is eating the event before onMenuItemClick() gets execute. I am running my app on nexus 5 android 5.0.1.

I find a solution for similar kind of problem from here. But I am not getting how to use this approach to my problem.

I tried using context menu instead of popup menu, but still I had the same message "Attempted to finish an input event but input event receiver has already been disposed" after clicking on the context menu item.

Please help me...!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've hit this problem from another angle; trying to launch a service from a menu. By default, the debug message is not too relevant. My solution was to eliminate filters in logcat and then I got another message that it could not launch the service in the first place (I forgot to put it in my manifest file).

In your case, you may need to wrap the toast display into a class:

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

and call it via a Handler object:

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

Or, even better, using Handler.postDelayed() method.

HTH,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...