600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > android @nonnull的作用 Android Studio错误的含义:未注释的参数将覆盖@NonNull参数

android @nonnull的作用 Android Studio错误的含义:未注释的参数将覆盖@NonNull参数

时间:2022-02-22 03:38:13

相关推荐

android @nonnull的作用 Android Studio错误的含义:未注释的参数将覆盖@NonNull参数

除了其他的答案, @NonNull (和它的对手, @Nullable )注释注解一个字段,参数或方法的返回types。 IntelliJ,因此Android Studio可以在编译时警告您可能的NullPointerException 。

这里最好的例子是:

@NonNull private String myString = "Hello"; @Nullable private String myOtherString = null; @NonNull public Object doStuff() { System.out.println(myString.length); // No warning System.out.println(doSomething(myString).length); // Warning, the result might be null. doSomething(myOtherString); // Warning, myOtherString might be null. return myOtherString; // Warning, myOtherString might be null. } @Nullable private String doSomething(@NonNull String a) { return a.length > 1 ? null : a; // No warning }

这些注释不会改变运行时行为(尽pipe我已经尝试过),但可以作为防止错误的工具。

请注意,您收到的消息不是错误,而只是一个警告,如果您愿意,可以放心忽略。 另一种方法是自己注释参数,如Android Studio所示:

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。