这个:
@Nullable
Item[] mItems;
public Item getItem(int position) {
return mItems[position];
}
产生警告:
Array access 'mItems[position]' may produce NullPointerException
我想取消这个警告(我知道如果 mItems
为空,则不会调用 getItem()
。
我尝试过使用以下注释:
@SuppressWarnings({"NullableProblems"})
@SuppressWarnings({"null"})
以及 //noinspection
表示法,但它们都不起作用。
使用 @SuppressWarnings({"all"})
有效,但显然不是我想要的。
当我点击 alt + enter 时,Android Studio 不提供任何抑制选项,只提供添加(无用)空检查的选项。
最佳答案
这对我有用,但不确定 AS 为什么要使用恒定条件作为抑制器。我认为它与跳过空检查有关,因为它是一个恒定条件(即,它永远不会为空)。
@Nullable
Item[] mItems;
@SuppressWarnings("ConstantConditions")
public Item getItem(int position) {
return mItems[position];
}
关于java - 抑制 Android Studio 中潜在的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931812/