我正在试用 Android Studio。在创建新项目并将默认 onSaveInstanceState
方法添加到 create MyActivity 类时,当我尝试将代码提交到 Git 时,我收到一个我不明白的奇怪错误。代码是这样的:
我得到的错误是这样的:
如果我尝试将方法签名更改为 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/