Android - 上下文菜单中的 v.getId() 返回“false”
我有一个上下文菜单。函数 v.getId() 应该返回 id,但实际上没有返回 - 相反,它返回“false”。
这是我的代码:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options");
menu.add(0, v.getId(), 0, v.getId());
menu.add(0, v.getId(), 0, "Save Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
//Stuff
}
public void function2(int id){
if (id == R.raw.tedcake){
Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
}
这些按钮是在代码中其他地方注册的上下文菜单。
为什么会返回 false?
谢谢。
I have a context menu. The function v.getId() is supposed to return the id, but does not - instead it returns 'false'.
Here is my code:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options");
menu.add(0, v.getId(), 0, v.getId());
menu.add(0, v.getId(), 0, "Save Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
//Stuff
}
public void function2(int id){
if (id == R.raw.tedcake){
Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
}
The buttons were registered for a context menu elsewhere in the code.
Why is it returning false?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题...在文件夹 res/values 中,您应该有一个文件 ID,每个项目有四个属性(名称、类型、格式、值)。就我而言,字段“值”为“假”
I had the same issue...in the folder res/values you should have a file ids, for each item there are four attributes (name, type, format, value). In my case the field "value" was "false"
view.getId()
返回一个 int,而不是 false。如果视图实际上没有 ID,则返回 NO_ID,这是一个等于 -1 的常量。我在您的代码中看到的一个问题是您正在使用 == 比较字符串。你应该使用 equals 来代替。如果您想了解更多信息,有很多相关的 SO 帖子。view.getId()
returns an int, not false. If the view does not actually have an ID, it returns NO_ID, which is a constant equal to -1. One issue I see with your code is that you are comparing strings using ==. You should use equals instead. There's plenty of related SO posts about that, if you want to know more.v.getId()
不可能返回false
。它返回一个整数,它永远不会是布尔值。menu.add(0, v.getId(), 0, v.getId());
。您在最后一个参数中传递v.getId()
,这会导致问题(因为它正在等待字符串资源标识符,而不是视图资源标识符)。也许您的意思是menu.add(0, v.getId(), 0, "Save Ringtone");
?关于你的问题:“为什么我会说谎?”。原因是:
这是新 Java 开发人员的常见问题。问题就在这里:
item.getTitle()=="Save Ringtone"
;具体来说,问题是==
。 这不是在 Java 中比较对象的方式。您必须使用equals
方法,如下所示:如果您对对象使用
==
运算符,则您将比较对对象的引用,而不是对象本身。v.getId()
returnsfalse
. It returns an integer, which won't be a boolean ever.menu.add(0, v.getId(), 0, v.getId());
. You are passingv.getId()
in the last parameter, which will cause problems (since it's waiting for an String resource identifier, not a view resource identifier). Maybe you meantmenu.add(0, v.getId(), 0, "Save Ringtone");
?With regards to your question: "why am I getting false?". It's because of this:
It's a common problem for new Java developers. The problem is here:
item.getTitle()=="Save Ringtone"
; specifically the problem is==
. That's not the way you compare objects in Java. You have to use theequals
method, this way:If you use the
==
operator with objects, you are comparing the references to the object, not the object.这是因为android中的大多数函数都采用string而不是int,toast也是如此。
尝试String.valueOf(view.getId())来祝你会看到id。别客气
it's because most functions in android take string not int, so does toast.
try String.valueOf(view.getId()) to toast u will see the id. ur welcome
我和你有类似的问题。
view.getID()
将始终返回一个整数,因此我决定看看如果将整数转换为 toast 中的字符串会发生什么。此方法将显示 ID。我相信您看到 false 的原因是因为当您提供整数而不是字符序列时,toast 会出错。I had a similar issue to you.
view.getID()
will always return an integer, so I decided to see what would happen if I cast the integer to a string in the toast. This method will show you the ID. I believe the reason you see false is because the toast errors out when you supply an integer instead of a char sequence.