且构网

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

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

更新时间:2021-10-04 21:16:19

是一个注解,但正确的名字是NonNull:

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.

如果已知局部变量为空(例如因为某些较早的代码检查它是否为空),然后将其作为参数被标记为@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.

在这种情况下,特别警告是您要覆盖的原始方法(在 Activity 中)在 outState上有一个 @NonNull 注释code> 参数,但您没有将其包含在覆盖方法中.只需添加它就可以解决问题,即

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