我发现了一个有趣的 Lint 检查,但我无法解释。我知道 Java 中隐藏的疯狂的东西,所以我想了解这里发生了什么。也许我可以用 super Nerd 的知识给我的 friend 留下深刻的印象。
以下是有问题的代码 fragment :
public void awesomeMethod()
{
final int[] hungryPeople = getHungryPeople();
if ( hungryPeople != null ) // <---- Lint says that this can never be null?
{
solveWorldHunger( true );
tellEveryone( false );
}
}
这是相关的(相关)方法:
public int[] getHungryPeople()
{
// Get hungry people data from the class's Android Intent field
final int[] values = mIntent.getIntArrayExtra( HUNGRY_PEOPLE );
if ( null != values )
{
return Arrays.copyOf( values, values.length );
}
return null;
}
我在这里缺少什么吗?
最佳答案
您在方法外部返回 null
,您需要在 getHungryPeople()
内部返回,以防 values
为 null
.
public int[] getHungryPeople()
{
// Get hungry people data from the class's Android Intent field
final int[] values = mIntent.getIntArrayExtra( HUNGRY_PEOPLE );
if ( null != values )
{
return Arrays.copyOf( values, values.length );
}
// if values is null, return null
return null;
}
关于Java 代码检查显示返回 null 的方法从不为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27005835/