以下代码段在Android Studio中生成lint警告.
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString(REDIRECT_KEY) != null) {
switch (extras.getString(REDIRECT_KEY)) { ...
Run Code Online (Sandbox Code Playgroud)
extras.getString(REDIRECT_KEY) 发出警告
取消引用'extras.getString(REDIRECT_KEY)'可能会产生'java.lang.NullPointerException'
但我没有看到任何可能发生这种情况的情况.它是lint检查中的一个错误,它只是不能识别我之前的if中的空检查吗?或者我会错过什么?
编辑:将代码更改为以下,确实删除了警告
if(getIntent() != null && getIntent().getStringExtra(REDIRECT_KEY) != null){
switch (getIntent().getStringExtra(REDIRECT_KEY)){
...
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是因为方式,这种棉绒检查工作(至少我猜).如果我在这张支票上显示更多信息,它会在某一时刻说出来
标记为@Nullable或@NotNull的变量,方法参数和返回值被视为可空(或分别为非空),并在分析期间用于检查可空性合同
查看Bundle和Intent的源代码显示:
/**
* Retrieve extended data from the intent.
*
* @param name The name of the desired item.
*
* @return the value of an item that previously added with putExtra()
* or null if no String value was found.
* …Run Code Online (Sandbox Code Playgroud)