我遇到了這個奇怪的問題。我完全按照http://developer.android.com/guide/topics/ui/menus.html#context-menu。我已經在單獨的虛擬應用程序上測試過它,它工作正常。問題是「窗口已經集中,忽略焦點收益:[email protected]」每當我嘗試點擊任何上下文菜單項後,按住任何列表項。 「onContextItemSelected」方法永遠不會被調用。(Android 2.1)「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市場免費的不適合我)。