无法对基本类型 boolean 调用 getItemId()
我是 Android 开发新手,我在网上搜索了大部分菜单选项示例。我正在尝试使用“getItemId”处理简单的单击事件,但出现以下错误:
无法对基本类型 boolean 调用 getItemId()。
代码如下:
public boolean onOptionsItemSelected(MenuItem menu)
{
// Handle Selection of Menu Items
switch (item.getItemId())
{
case(R.id.refresh):Toast.makeText(this, "Refresh", Toast.LENGTH_LONG).show();
break;
case(R.id.info):Toast.makeText(this, "Info", Toast.LENGTH_LONG).show();
break;
}
return true;
提前感谢您的帮助!
I am new to android development and I have searched most of Menu option examples on the web. I am trying to handle a simple click event using "getItemId" and I get the following error :
Cannot invoke getItemId() on the primitive type boolean.
Here's the code :
public boolean onOptionsItemSelected(MenuItem menu)
{
// Handle Selection of Menu Items
switch (item.getItemId())
{
case(R.id.refresh):Toast.makeText(this, "Refresh", Toast.LENGTH_LONG).show();
break;
case(R.id.info):Toast.makeText(this, "Info", Toast.LENGTH_LONG).show();
break;
}
return true;
Thanks in advance for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是这个:
而是:
失败的原因正如错误所述。布尔值是基元,而不是对象。我假设这段摘录上方的某处是
boolean item;
行。如果您对布尔值进行切换,唯一可能的情况是true
和false
。使用
menu
而不是item
- 这是作为参数传递给此方法的 menuItem。不幸的是,我不知道 *.getItemId() 是否适用于
menuItem
。我没试过。如果没有,请告诉我,我会帮助你找到其他方法。not this:
but:
The reason this fails is just as the error says. Booleans are primitives, not objects. I am assuming somewhere above this excerpt is the line
boolean item;
. If you did a switch on a boolean the only possible cases would betrue
andfalse
.Instead of
item
, usemenu
- this is the menuItem passed to this method as a parameter.And unfortunately, I don't know if *.getItemId() will work on a
menuItem
. I haven't tried it. If it doesn't, let me know, and I'll help ya find another way.