Java Android Studio 错误的含义:未注释的参数覆盖@NonNull 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24728627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter
提问by Monomo
I'm trying out Android Studio. Upon creating a new project and adding a default onSaveInstanceState
method to the create MyActivity class, when I try to commit the code to Git, I get a strange error I don't understand. The code is this:
我正在试用 Android Studio。在创建新项目并向onSaveInstanceState
create MyActivity 类添加默认方法后,当我尝试将代码提交到 Git 时,出现一个我不明白的奇怪错误。代码是这样的:
The error I get is this:
我得到的错误是这样的:
If I try to change the method signature to protected void onSaveInstanceState(@NotNull Bundle outState)
, then the IDE tells me it can't resolve the symbol NotNull
.
如果我尝试将方法签名更改为protected void onSaveInstanceState(@NotNull Bundle outState)
,则 IDE 会告诉我它无法解析符号NotNull
。
What do I need to do to get rid of the warning?
我需要做什么才能摆脱警告?
回答by matiash
It's an annotation, but the correct name is NonNull
:
这是一个注释,但正确的名称是NonNull
:
protected void onSaveInstanceState(@NonNull Bundle outState)
(And also)
(并且)
import android.support.annotation.NonNull;
The purpose is to allow the compilerto warn when certain assumptions are being violated (such as a parameter of a method that should always have a value, as in this particular case, although there are others). From the Support Annotationsdocumentation:
目的是允许编译器在违反某些假设时发出警告(例如,方法的参数应该始终具有值,在这种特殊情况下,尽管还有其他值)。从支持注释文档:
The
@NonNull
annotation can be used to indicate that a given parameter can not be null.If a local variable is known to be null (for example because some earlier code checked whether it was null), and you pass that as a parameter to a method where that parameter is marked as @NonNull, the IDE will warn you that you have a potential crash.
该
@NonNull
注释可以用来表示一个给定参数不能为空。如果已知局部变量为空(例如,因为某些较早的代码检查了它是否为空),并且您将其作为参数传递给将该参数标记为 @NonNull 的方法,IDE 将警告您潜在的崩溃。
They are tools for static analysis. Runtime behavior is not altered at all.
它们是静态分析的工具。运行时行为根本没有改变。
In this case, the particular warning is that the original method you're overriding (in Activity
) has a @NonNull
annotation on the outState
parameter, but you did not include it in the overriding method. Just adding it should fix the issue, i.e.
在这种情况下,特别警告是您要覆盖的原始方法 (in Activity
)@NonNull
在outState
参数上有一个注释,但您没有将它包含在覆盖方法中。只需添加它就可以解决问题,即
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}
回答by LukaCiko
A number of useful support annotationswere recently added in the Android support library. Their primary role is to annotate properties of various methods and parameters to help catch bugs. For example, if you pass null
value to a parameter that is marked with the NotNull
annotation you will get a warning.
最近在 Android 支持库中添加了许多有用的支持注释。它们的主要作用是注释各种方法和参数的属性以帮助捕获错误。例如,如果您将null
值传递给标有NotNull
注释的参数,您将收到警告。
The annotations can be added to your project with Gradle by adding the following dependency:
通过添加以下依赖项,可以使用 Gradle 将注释添加到您的项目中:
dependencies {
compile 'com.android.support:support-annotations:20.0.0'
}
You are getting the warning because the Bundle
parameter is marked with the @NotNull
annotation and by overriding the method the annotation gets hidden. The right thing to do is to add the annotation to the overriden method's parameter as well.
您收到警告是因为Bundle
参数用@NotNull
注释标记,并且通过覆盖注释隐藏的方法。正确的做法是也将注释添加到覆盖方法的参数中。
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}
回答by nhaarman
In addition to the other answers, the @NonNull
(and it's opponent, @Nullable
) annotation annotates a field, parameter or method return type. IntelliJ and thus Android Studio can warn you for possible NullPointerException
s at compile time.
除了其他答案之外,@NonNull
(和它的对手,@Nullable
)注释注释字段、参数或方法返回类型。IntelliJ 和 Android Studio 可以NullPointerException
在编译时警告你可能的s。
An example is best here:
这里有一个最好的例子:
@NonNull private String myString = "Hello";
@Nullable private String myOtherString = null;
@NonNull
public Object doStuff() {
System.out.println(myString.length); // No warning
System.out.println(doSomething(myString).length); // Warning, the result might be null.
doSomething(myOtherString); // Warning, myOtherString might be null.
return myOtherString; // Warning, myOtherString might be null.
}
@Nullable
private String doSomething(@NonNull String a) {
return a.length > 1 ? null : a; // No warning
}
These annotations do not alter runtime behavior (although I have experimentedwith this), but serve as a tool for preventing bugs.
这些注释不会改变运行时行为(尽管我已经对此进行了试验),而是用作防止错误的工具。
Note that the message you received was not an error, but just a warning, which is safe to ignore, if you choose to. The alternative is to annotate the parameter yourself as well, as Android Studio suggests:
请注意,您收到的消息不是错误,而只是警告,如果您愿意,可以安全地忽略。另一种方法是自己注释参数,正如 Android Studio 建议的那样:
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}