[英]Android Option Menu not working properly; what am I missing?
I've been away from Java for some time --functional programming has been my muse-- and recently decided to jump back in with an android application. 我已经离开Java一段时间了 - 函数式编程一直是我的缪斯 - 最近决定重新使用android应用程序。 Things are going well.
事情进展顺利。 Javas syntax is mostly back in my brain, OO design principles are a little rusty, but I'm not afraid of re-factoring.
Javas的语法大部分都回到了我的脑海,OO的设计原则有点生疏,但我并不害怕重新分解。
One problem I hit has been with the option menus in the platform. 我遇到的一个问题是平台中的选项菜单。 I load them from an XML file through a menu-inflator in my main activity (below), and I can see them!
我在主要活动(下面)中通过菜单充气器从XML文件加载它们,我可以看到它们! But, when I press them things get weird --but not like seeing your grandmother make-out with your best friend, much less weird.
但是,当我按下它们时,事情会变得奇怪 - 但不像看到你的祖母与你最好的朋友一起做出来,更不用说怪异了。
For some reason, when I press the first button, I get the friendly default
message in the code sample below, "That's not an option, moron!". 出于某种原因,当我按下第一个按钮时,我在下面的代码示例中得到了友好的
default
消息,“那不是一个选项,白痴!”。 And when I press the second, the message is "Adding One". 当我按下第二个时,消息是“添加一个”。 I'm off by one somehow!
不知怎的,我不知所措! But, but how!?
但是,但是如何!? but why!?
但为什么!?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add_single_id"
android:title="@string/add_one" />
<item android:id="@+id/add_multi_id"
android:title="@string/add_multi" />
</menu>
... which is loaded by the menu inflator... ...由菜单充气器加载......
public boolean onCreateOptionsMenu( Menu menu ){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
... and finally the listener for items selected. ...最后是所选项目的监听器。
public boolean onOptionsItemSelected( MenuItem item ){
switch( item.getItemId() ){
case R.id.add_single_id:
Toast.makeText(this, "Adding One", Toast.LENGTH_LONG).show();
add_single();
break;
case R.id.add_multi_id:
Toast.makeText(this, "Adding n", Toast.LENGTH_LONG).show();
Intent i = new Intent(this, SelectMulti.class);
startActivityForResult(i, ACTIVITY_LOADMULTI);
break;
default:
Toast.makeText(this, "That's not an option, moron!", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
当我在eclipse上开发android时,这种情况发生在我身上,并且清理和重建项目修复它,因为它将重新创建android资源文件并正确映射到您的UI id。
在您的XML中,您有ID = add_one_id但在代码中使用了R.id.add_single_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.