如何摆脱 Android Studio 上的可疑来电警告?

How to get rid of Suspicious call warning on Android Studio?

在我的代码中,我有一个名为 mButtonsArrayList<Buttons> 字段。 这些按钮中的每一个都调用(在 XML 中)相同的 onClick 函数 onButtonClick。 该函数如下所示:

public void onButtonClick(View view) {
    int buttonIndex = mButtons.indexOf(view);
}

但是 Android Studio 一直警告我 Suspicious call to 'ArrayList.indexOf'

好的,我试图通过将 view 转换为 Button 来摆脱困境。 然后警告变成了Casting 'view' to 'Button' is redundant.

好吧,我尝试更改函数签名以接收 Button 而不是 View。 但是现在我对每个 Button 声明 (XML) 都有一个警告:Method 'onButtonClick' on '...Activity' has incorrect signature.

我真的在考虑只添加 //noinspection SuspiciousMethodCalls,因为它似乎没有解决方法。

如果有人知道如何摆脱它,我将不胜感激。

您可以转换到 Button 之前的行。

Button button = (Button) view;
int buttonIndex = mButtons.indexOf(button);

作为额外的答案,您只需将调用包装在 instanceof check

if (view instanceof Button) {
    buttonIndex = mButtons.indexOf(button);
}

基本上可疑电话是说"There's no guarantee this is a valid object to look for"

(抱歉回复慢,我在寻找答案时点击了这个线程,我认为提供替代解决方案是最好的)