Android Studio "Might Be Null" URLUtil.isValidUrl() 的警告但 TextUtils.isEmpty() 的警告
Android Studio "Might Be Null" Warning for URLUtil.isValidUrl() but not for TextUtils.isEmpty()
我想知道为什么会这样:
请注意,两个 if 语句的顺序不会改变行为。
如果您查看这两个检查器函数的源代码,我看不出两者有任何显着差异。那么为什么我只在第二种情况下收到警告?
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
public static boolean isValidUrl(String url) {
if (url == null || url.length() == 0) {
return false;
}
return (isAssetUrl(url) ||
isResourceUrl(url) ||
isFileUrl(url) ||
isAboutUrl(url) ||
isHttpUrl(url) ||
isHttpsUrl(url) ||
isJavaScriptUrl(url) ||
isContentUrl(url));
}
更新:解决方案:
由于 TextUtils.isEmpty() 在参数上有一个 @Nullable 标记,因此适用以下内容:
Variables, method parameters and return values marked as @Nullable or
@NotNull are treated as nullable (or not-null, respectively) and used
during the analysis to check nullability contracts, e.g. report
possible NullPointerException errors.
您应该注意到 TextUtils.isEmpty 参数使用 @Nullable 注释进行注释。 UrlUtil.isValidUrl 没有这个注解。
我想知道为什么会这样:
请注意,两个 if 语句的顺序不会改变行为。
如果您查看这两个检查器函数的源代码,我看不出两者有任何显着差异。那么为什么我只在第二种情况下收到警告?
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
public static boolean isValidUrl(String url) {
if (url == null || url.length() == 0) {
return false;
}
return (isAssetUrl(url) ||
isResourceUrl(url) ||
isFileUrl(url) ||
isAboutUrl(url) ||
isHttpUrl(url) ||
isHttpsUrl(url) ||
isJavaScriptUrl(url) ||
isContentUrl(url));
}
更新:解决方案:
由于 TextUtils.isEmpty() 在参数上有一个 @Nullable 标记,因此适用以下内容:
Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors.
您应该注意到 TextUtils.isEmpty 参数使用 @Nullable 注释进行注释。 UrlUtil.isValidUrl 没有这个注解。