Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用菜单项在活动之间切换时出错_Android_Eclipse_Android Intent_Menu_Menuitem - Fatal编程技术网

在Android中使用菜单项在活动之间切换时出错

在Android中使用菜单项在活动之间切换时出错,android,eclipse,android-intent,menu,menuitem,Android,Eclipse,Android Intent,Menu,Menuitem,我正在尝试使用菜单项在应用程序中的活动之间切换。不幸的是,当我点击菜单项时,它什么也不做 下面是activity.java代码: @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);

我正在尝试使用菜单项在应用程序中的活动之间切换。不幸的是,当我点击菜单项时,它什么也不做

下面是activity.java代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
// Called when the user selects a contextual menu item
@Override
public boolean onContextItemSelected(MenuItem item) {
    //Handles Item Selection.
    switch (item.getItemId()) {
        case R.id.action_switch_natural:
            Intent a = new Intent(this, Natural_Display.class);
            startActivity(a);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}
以下是main.xml的代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_switch_natural"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_switch_natural"/>

</menu>


我做错了什么?

我认为您必须在选择OnContextItem之前覆盖onCreatecontextMenu

试试这个

@覆盖
public void onCreateContextMenu(上下文菜单, 视图v,上下文菜单信息(菜单信息){


}
onContextItemSelected(MenuItem项目)
应该是
OnOptions ItemSelected(MenuItem项目)
,如图所示。不要忘记将
super.onContextItemSelected(item)
更改为
super.onOptions ItemSelected(item)

super.onCreateContextMenu(menu, v, menuInfo);

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.context_menu, menu);