gpt4 book ai didi

java - 如何摆脱 Android Studio 上的可疑调用警告?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:20 28 4
gpt4 key购买 nike

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

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

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

好吧,我试图通过施法来摆脱 viewButton .然后警告更改为 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);

关于java - 如何摆脱 Android Studio 上的可疑调用警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009083/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com