在Android中使用菜单项在活动之间切换时出错
我正在尝试使用菜单项在应用程序中的活动之间切换。不幸的是,当我点击菜单项时,它什么也不做 下面是activity.java代码:在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);
@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);