Android Studio 错误的含义: 没有注释的参数覆盖@NonNull 参数

我在试用安卓工作室。在创建一个新项目并将一个默认的 onSaveInstanceState方法添加到创建 MyActivity 类时,当我试图将代码提交到 Git 时,我得到了一个我不明白的奇怪错误。密码是这样的:

我得到的错误是:

enter image description here

如果尝试将方法签名更改为 protected void onSaveInstanceState(@NotNull Bundle outState),则 IDE 告诉我它无法解析符号 NotNull

我要怎么做才能摆脱警告?

49118 次浏览

It's an annotation, but the correct name is NonNull:

protected void onSaveInstanceState(@NonNull Bundle outState)

(And also)

import android.support.annotation.NonNull;

The purpose is to allow the compiler to 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 Annotations documentation:

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.

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.

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}

A number of useful support annotations were 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.

The annotations can be added to your project with Gradle by adding the following dependency:

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.

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}

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 NullPointerExceptions at compile time.

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 experimented with 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:

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}