【问题标题】:Android Studio gives redundant cast warning, but it's not really redundantAndroid Studio 给出了多余的演员表警告,但实际上并不是多余的
【发布时间】:2015-01-04 23:14:54
【问题描述】:

在我的代码中我调用:

myArray[ (Integer) compoundButton.getTag() ] = true;

它有效。然而,Android Studio 给了我一个警告,说强制转换为 (Integer) 是多余的。但是,如果我删除强制转换,由于 int 和 java.lang.Object 类型不兼容,它会引发错误。我是否做错了什么?我该怎么做才能消除此警告?

【问题讨论】:

  • 当您查看消息时,Android Studio 说什么来纠正它?此外,运行检查:Analyze -> Inspect Code,它很可能会为您修复它。
  • @JaredBurrows 我在分析时遇到了问题 -> 检查代码,谢谢。

标签: java android


【解决方案1】:

不,您的代码没有任何内容,显然这里 AS 是错误的,在我的机器上结果相同,与 Android 无关。

int[] ints = new int[] {};
Object foo = new Object();
ints[((int) foo)] = 1; // complains

(您可能想要转换为 int(而不是 Integer),但这不会解决问题。)

顺便说一句,如果你提取一个变量

int indexFromTag = (int) button.getTag();
myArray[indexFromTag];

AS 不会抱怨的。

【讨论】:

  • 嗯,我很高兴知道我不是唯一一个遇到这个问题的人。提取作品以消除那个讨厌的错误,谢谢。
  • 真的很奇怪。 ints[((Integer) foo)] = 1; 不行,但 System.out.println(ints[((Integer) foo)]); 显然是。
猜你喜欢
  • 2016-03-06
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2012-06-03
  • 2012-02-11
相关资源
最近更新 更多