1

我正在以与我一直这样做相同的方式编写代码并且看不到我错在哪里:

@Override
public void onCreate(){
   ...
   this.registerForContextMenu(lv);

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.context_menu_device_item_remove, menu);
}

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:id="@+id/context_menu_item_remove_id"
           android:title="Remove" />
     <item android:id="@+id/context_menu_item_clear_all_id"
           android:title="Clear all" />
</menu>

如您所见..效果是:

在此处输入图像描述

由于使用了 AdapterContextMenuInfo.position,应用程序在单击第三个或第四个元素时崩溃:

@Override
public boolean onContextItemSelected(MenuItem item) {
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
     System.out.println("## info.position: "+info.position);
...

你有没有遇到过这种情况?& 你是如何逃脱的?

4

2 回答 2

0

显然 BUG 是由于有 2 个电话this.registerForContextMenu(lv);(晚餐班的第一个).. 所以是的,我的错。

于 2012-10-03T10:00:55.127 回答
-1

试试下面的代码:

     public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    menu.add(0, MENU_ITEM_SEND_MSG, 0, "Send a Message");
    menu.add(0, MENU_ITEM_MAKE_A_CALL, 0, "Make a Call");

}


     public boolean onContextItemSelected(MenuItem item) {

    SharedPreferences server_sp = getApplicationContext()
            .getSharedPreferences("server", MODE_PRIVATE);
    String server = server_sp.getString("Server", "server");

    switch (item.getItemId()) {
    case MENU_ITEM_SEND_MSG:
           //do ur stuff

    case MENU_ITEM_MAKE_A_CALL:

        //do ur stufff
        break;
    }
    return false;
}
于 2012-10-02T14:57:39.100 回答