Android Studio error value: non-annotated parameter overrides @NonNull parameter

I am testing Android Studio. When I create a new project and add the default onSaveInstanceState method for the MyActivity class, when I try to pass the code to Git, I get a strange error that I do not understand. The code looks like this:

The error I am getting is this:

enter image description here

If I try to change the method signature to protected void onSaveInstanceState(@NotNull Bundle outState) , then the IDE tells me that it cannot resolve the NotNull character.

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

+63
java compiler-construction android android-studio
Jul 14 '14 at 1:58
source share
3 answers

This is an annotation, but the correct name is NonNull :

 protected void onSaveInstanceState(@NonNull Bundle outState) 

(And also)

 import android.support.annotation.NonNull; 

The goal is to allow the compiler to warn when certain assumptions are violated (for example, a method parameter that should always have a value, as in this particular case, although there are others). In the documentation Support for annotations :

The @NonNull can be used to indicate that this parameter cannot be null.

If the local variable is known as null (for example, since some earlier code checked to see if it was null), and you pass it as a parameter to a method where this parameter is marked as @NonNull, the IDE will warn you that you have a potential crash.

These are tools for static analysis. Runtime behavior does not change at all.




In this case, a special warning is that the original method that you override (in Activity ) has the @NonNull annotation in the outState parameter, but you did not include it in the override method. Just add it to fix the problem i.e.

 @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); } 
+81
Jul 14 '14 at 3:18
source share

Recently, several useful support annotations have been added to the Android support library. Their main role is to annotate the properties of various methods and parameters to help catch errors. For example, if you pass null to a parameter marked with the NotNull annotation, you get a warning.

Annotations can be added to your project using Gradle by adding the following dependency:

 dependencies { compile 'com.android.support:support-annotations:20.0.0' } 

You get a warning because the Bundle parameter is marked with the @NotNull annotation and overrides the method with which the annotation is hiding. The right thing is to add annotation to the parameter of the overridden method.

 @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); } 
+10
Oct. 15 '14 at 20:46
source share

In addition to other answers, the @NonNull annotation (and its adversary, @Nullable ) annotates the type of the returned field, parameter, or method. IntelliJ and thus Android Studio can warn you of a possible NullPointerException 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 change runtime behavior (although I experimented with this), but serve as a tool to prevent errors.

Please note that the message received was not an error, but simply a warning that can be ignored if you decide. An alternative is also to annotate the parameter yourself, as Android Studio suggests:

 @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); } 
+6
Oct. 15 '14 at 20:59
source share



All Articles