且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的工作室错误含义:未注明参数覆盖@NonNull参数

更新时间:2021-12-13 04:32:58

这是一个注释,但正确的名称是非空

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

protected void onSaveInstanceState(@NonNull Bundle outState)

(和)

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:

@NonNull 标注可以用于指示给定的参数   不能为空。

The @NonNull annotation can be used to indicate that a given parameter can not be null.

如果局部变量是已知为空(例如,由于一些   早前code检查是否为空),并传递,作为一个   参数的方法,其中该参数标记为@NonNull,所述   IDE会警告你,你有一个可能的撞击。

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.

在这种情况下,特定​​的警告是,你覆盖(在活动)的原始方法有一个 @NonNull 注释上的 outState 参数,但不包括它的首要方法。只需添加它应该解决这个问题,即。

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