java - Android Studio 错误 : Not annotated parameter overrides @NonNull parameter 的含义

标签 java android android-studio compiler-construction annotations

我正在试用 Android Studio。在创建新项目并将默认 onSaveInstanceState 方法添加到 create MyActivity 类时,当我尝试将代码提交到 Git 时,我收到一个我不明白的奇怪错误。代码是这样的:

我得到的错误是这样的:

enter image description here

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

我需要做什么才能消除警告?

最佳答案

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

protected void onSaveInstanceState(@NonNull Bundle outState)

(还有)

import android.support.annotation.NonNull;

目的是允许编译器在违反某些假设时发出警告(例如,在这种特殊情况下,应该始终具有值的方法的参数,尽管还有其他假设)。来自 Support Annotations文档:

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.

它们是用于静态分析的工具。运行时行为根本没有改变。


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

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

关于java - Android Studio 错误 : Not annotated parameter overrides @NonNull parameter 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728627/

相关文章:

java - 使用 java 在 JPA 中插入并获取作为 AM/PM 的时间查询

java - 构建文件夹紧凑树的最佳方法是什么?

android - 并非自动授予 ACCOUNT 权限组中的所有权限

java - 如何使用 Stanford CoreNLP Coreferences 模块通过最具代表性的提及来替换单词

java - 在Java中过滤列表并将结果放入新的数组列表中

android - 空间不足时使用替代布局

java - 使用 currentTimeMillis 测量耗时返回 0

android - 如何重用 Gradle 中的子模块?

Android Studio,Gradle 更新错误

android - Android Studio中自动保存文件历史记录