我想了Android的工作室。在创建一个新项目,并添加默认的onSaveInstanceState
方法的创建MyActivity类,当我尝试提交code到Git的,我得到一个奇怪的错误我不T理解。在code是这样的:
I'm trying out Android Studio. Upon creating a new project and adding a default onSaveInstanceState
method to the create MyActivity class, when I try to commit the code to Git, I get a strange error I don't understand. The code is this:
我得到的错误是这样的:
The error I get is this:
如果我尝试改变方法签名保护无效的onSaveInstanceState(@NotNull捆绑outState)
,则IDE告诉我它无法解析符号 NOTNULL
。
If I try to change the method signature to protected void onSaveInstanceState(@NotNull Bundle outState)
, then the IDE tells me it can't resolve the symbol NotNull
.
什么我需要做摆脱警告?
What do I need to do to get rid of the warning?
这是一个注释,但正确的名称是非空
:
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);
}