我遇到了这个奇怪的问题。我完全按照http://developer.android.com/guide/topics/ui/menus.html#context-menu。我已经在单独的虚拟应用程序上测试它,它工作正常。问题是“窗口已经集中,无视焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@xxxxxx”每当我在按住任何列表项后尝试单击任何上下文菜单项时。永远不会调用“onContextItemSelected”方法。
以下是精简代码:
TaskActivity.java
public class TaskActivity extends Activity{
private ListView task_list;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);//custom xml file of my GUI
task_list = (ListView) findViewById(R.id.listTasks);
populateTaskList();//snapped method
registerForContextMenu(task_list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
menu.setHeaderTitle("Options");
Log.v("CBC","HERE2");//It logs, yes
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("CBC","HERE1");//Not ever logged this message!
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit_menuitem:
update_task(info.id);
return true;
case R.id.delete_menuitem:
Log.v("CBC","HERE2");
delete_task(info.id);
return true;
}
return super.onContextItemSelected(item);
}
}
<snap irrelevant codes that have played no role in this problem>
activity_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white_bg"
android:orientation="vertical">
<snap irrelvant codes>
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selectors"
android:id="@+id/listTasks"/>
</LinearLayout>
task_row.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/row_selector"
android:padding="5dip">
<TextView android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dip"/>
<TextView android:id="@+id/mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dip"/>
</LinearLayout>
所以我想知道你是否知道这个奇怪的问题?如果您需要了解更多其他代码,请与我们联系。我向你们保证,他们不相关。
P.S。这是我写的第一个大型应用程序。 P.S.S.我只是在我的Android手机上为我的特殊需求编写这个小应用程序。 (Android市场免费的不适合我)。
答案 0 :(得分:0)
这解释了为什么你不能一起使用onMenuItemSelected方法和onContextItemSelected。
http://biginteger.blogspot.com/2010/09/solution-to-oncontextitemselected-not.html