Android - 上下文菜单中的 v.getId() 返回“false”

发布于 2024-09-27 22:25:27 字数 904 浏览 0 评论 0原文

我有一个上下文菜单。函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木槿暧夏七纪年 2024-10-04 22:25:27

我遇到了同样的问题...在文件夹 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"

梦里泪两行 2024-10-04 22:25:27

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.

那小子欠揍 2024-10-04 22:25:27
  1. v.getId() 不可能返回 false。它返回一个整数,它永远不会是布尔值。
  2. 另一方面,这应该做什么?:menu.add(0, v.getId(), 0, v.getId());。您在最后一个参数中传递 v.getId() ,这会导致问题(因为它正在等待字符串资源标识符,而不是视图资源标识符)。也许您的意思是 menu.add(0, v.getId(), 0, "Save Ringtone");

关于你的问题:“为什么我会说谎?”。原因是:

if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}  
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else return false;

这是新 Java 开发人员的常见问题。问题就在这里: item.getTitle()=="Save Ringtone";具体来说,问题是==这不是在 Java 中比较对象的方式。您必须使用 equals 方法,如下所示:

if(item.getTitle().equals("Save Ringtone")){function1(item.getItemId());}  
else if(item.getTitle().equals("Save Notification")){function2(item.getItemId());}
else return false;

如果您对对象使用 == 运算符,则您将比较对对象的引用,而不是对象本身。

  1. It's impossible that v.getId() returns false. It returns an integer, which won't be a boolean ever.
  2. On the other hand, what's this supposed to do?: menu.add(0, v.getId(), 0, v.getId());. You are passing v.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 meant menu.add(0, v.getId(), 0, "Save Ringtone");?

With regards to your question: "why am I getting false?". It's because of this:

if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}  
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else return false;

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 the equals method, this way:

if(item.getTitle().equals("Save Ringtone")){function1(item.getItemId());}  
else if(item.getTitle().equals("Save Notification")){function2(item.getItemId());}
else return false;

If you use the == operator with objects, you are comparing the references to the object, not the object.

此岸叶落 2024-10-04 22:25:27

这是因为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

于我来说 2024-10-04 22:25:27

我和你有类似的问题。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文