Android Studio 的“预期类型资源”检查?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24716385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:38:14  来源:igfitidea点击:

Android Studio's "expected resource of type" checks?

androidandroid-studiolinttypechecking

提问by matiash

Android Studio Beta (0.8) has a nifty new feature where it checks that some intparameters are not arbitrary integers, but rather have some properties.

Android Studio Beta (0.8) 有一个漂亮的新功能,它检查某些int参数不是任意整数,而是具有某些属性。

For example, calling something like:

例如,调用类似的东西:

setContentView(R.id.textView1);

will correctly report that R.id.textView1is not a layout id (the message is "expected resource of type layout"). There are other cases of this peppered around.

将正确报告这R.id.textView1不是布局 ID(消息是“预期的布局类型资源”)。还有其他类似的案例。

Understandably, this protection is lost as soon as you add your own methods into the mix, e.g.

可以理解的是,一旦您将自己的方法添加到组合中,这种保护就会丢失,例如

private void mySetContentView(int resourceId) {
    setContentView(resourceId);
}

I can then call mySetContentView()with any arbitrary integer and it will not complain.

然后我可以mySetContentView()用任何任意整数调用,它不会抱怨。

So, I have two (related) questions:

所以,我有两个(相关的)问题:

  1. How is this achieved -- are the special checks "baked" into lint?
  2. Is there any way to annotate the mySetContentView()method so that it will also report a resource type error when calling it with an invalid value?
  1. 这是如何实现的——特殊检查是否“烘焙”成棉绒?
  2. 有什么方法可以注释该mySetContentView()方法,以便在使用无效值调用它时也会报告资源类型错误?

回答by matiash

(Thanks to @CommonsWare for the heads up).

(感谢@CommonsWare 的提醒)。

There are Java annotations to support these checks in your own code. They can all be found in the android.support.annotationspackage:

在您自己的代码中有 Java 注释来支持这些检查。它们都可以在android.support.annotations包中找到:

  • IdRes
  • DrawableRes
  • LayoutRes
  • StringRes
  • &c
  • IdRes
  • DrawableRes
  • LayoutRes
  • StringRes
  • &C

In this case, for example, I could use:

例如,在这种情况下,我可以使用:

private void mySetContentView(@LayoutRes int resourceId) {
    setContentView(resourceId);
}

and Android Studio will check that the provided resource id is indeed for a layout.

并且 Android Studio 将检查提供的资源 ID 是否确实用于布局。

Moreover, these annotations are exported, so they can be especially useful when designing a library.

此外,这些注释是导出的,因此它们在设计库时特别有用。

Sources:

资料来源:

回答by Kitesurfer

This are all Annotations:

这是所有的注释:

@AnimatorRes
@AnimRes
@AnyRes
@ArrayRes
@AttrRes
@BoolRes
@ColorRes
@DimenRes
@DrawableRes
@FractionRes
@IdRes
@IntDef
@IntegerRes
@InterpolatorRes
@LayoutRes
@MenuRes
@NonNull
@Nullable
@PluralsRes
@RawRes
@StringDef
@StringRes
@StyleableRes
@StyleRes
@XmlRes

回答by Mahendran Candy

Try this answer: Its working... Put this code into your build.gradle

试试这个答案:它的工作......把这段代码放到你的 build.gradle 中

android {
 lintOptions {
    disable "ResourceType"
  }
}

回答by Volodymyr Yatsykiv

All annotations, that you can use with android.support.annotationyou can find here.

android.support.annotation您可以在此处找到所有可以与您一起使用的注释。

And technical docabout supporting annotations.

以及关于支持注释的技术文档