【发布时间】:2011-11-15 21:56:27
【问题描述】:
我的 Java 或 Android 项目中的这些行是什么意思?
@SuppressWarnings("deprecation")
@SuppressWarnings("unused")
【问题讨论】:
标签: java android suppress-warnings
我的 Java 或 Android 项目中的这些行是什么意思?
@SuppressWarnings("deprecation")
@SuppressWarnings("unused")
【问题讨论】:
标签: java android suppress-warnings
@SuppressWarnings 注释禁用某些编译器警告。在这种情况下,关于不推荐使用的代码 ("deprecation") 和未使用的局部变量或未使用的私有方法 ("unused") 的警告。 This article explains the possible values.
【讨论】:
还有一件事:您不仅可以内联添加它们,还可以添加annotate methods。例如
@Override
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
但是,建议使用尽可能小的范围
作为一种风格,程序员应该始终在最有效的嵌套元素上使用这个注解。如果您想禁止在特定方法中显示警告,则应注释该方法而不是其类。
【讨论】:
在 Java 中,@SuppressWarnings 用于限制编译器在控制台屏幕上显示某些警告。
例如
@SuppressWarnings("unused")
CheckBox transferredField = new CheckBox("is transferred");
如果我不在代码中使用 transferredField 变量,那么您的 Eclipse IDE 永远不会显示您没有在代码中使用此 transferredField 变量的警告。
【讨论】:
@SuppressWarnings("unused") 和其他的我都试过了,但是都不管用,我总是用这个项目中说 gradle 的方法来
lintOptions{
checkReleaseBuilds false
abortOnError false;
disable 'deprecation'
}
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
【讨论】: