1

看看下面的代码......为什么我的上下文菜单没有被创建???这可能是什么原因......?

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflator = getMenuInflater();

    inflator.inflate(R.menu.contextmenudisplayer, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.delete:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

          String s = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(2);
          Log.v("ID is", s);

    default:

    return super.onContextItemSelected(item);
    }
}

这是上下文菜单显示器中 xml 文件的代码 contextmenudisplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/delete"
        android:title="Delete Message">
    </item>
    <item
        android:id="@+id/forward"
        android:title="Forward">
    </item>

</menu>
4

1 回答 1

3

您需要使用此方法注册菜单:http: //developer.android.com/reference/android/app/Activity.html#registerForContextMenu (android.view.View )

阅读此页面,它解释了一切:http: //developer.android.com/guide/topics/ui/menus.html

于 2012-04-10T10:44:21.030 回答