ChatGPT解决这个技术问题 Extra ChatGPT

Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter

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:

https://i.stack.imgur.com/bxyek.png

The error I get is this:

https://i.stack.imgur.com/qJYSE.png

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.

What do I need to do to get rid of the warning?


m
matiash

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);
}

What is the purpose of it?
@IgorGanapolsky Sorry, hadn't mentioned that because I assumed the question was only about the NotNull/NonNull difference. Adjusted answer accordingly.
In other word, IMHO, this annotation can remove the necessary of null-checking inside a function, and have a faster code.
@JohnPang You could, but since the restriction implied by the annotation isn't guaranteed to be actually enforced, it might not be a good idea.
import android.support.annotation.NonNull; looking for this thing for 2 hours... not one mentioned how to import NonNull.. hence upvote
L
LukaCiko

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);
}

n
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 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);
}