问题
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...!!
回答1:
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,
回答2:
You have to pass the R.layout.id in the super() to prevent this error. For example:
public PersonAdapter(Context c, ArrayList<String> list) {
super(c, R.layout.item_layout, list);
}
来源:https://stackoverflow.com/questions/27588750/attempted-to-finish-an-input-event-but-input-event-receiver-has-already-been-dis