android注释语法

时间:2016-04-16 21:46:03

标签: android android-resources android-annotations

我知道这是一个愚蠢的问题但是

我从未使用过android Annotation,它只是在android studio中出现了自动完成,我认为使用它是一个好主意,我是对的吗?

我完成了我的研究,并了解了如何将其与parameters一起使用,但我没有找到任何关于如何在pairgeneric

中使用它的信息

这是我的代码

private Pair<StringRes ,DrawableRes> getRespectiveStingAndDrawableRes(String permission) {
        if(permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            return Pair.create(R.string.WRITE_EXTERNAL_STORAGE_permision_message,R.drawable.ic_menu_gallery);
        return null;
    }

出现此错误

Error:(49, 31) error: incompatible types
required: Pair<StringRes,DrawableRes>
found:    Pair<Integer,Integer>

我尝试使用IntegerRes但也没有用

注意:我使用的piarandroid.support.v4.util.Pair

2 个答案:

答案 0 :(得分:0)

我不确定你的问题与注释有什么关系,因为你没有使用任何注释,但错误指的是R.stringR.drawable都是int值,而不是StringRes或DrawableRes

这可能有用。注意@XRes是注释,它不是类似于您尝试使用它的类类型。

private Pair<Integer , Integer> getRespectiveStingAndDrawableRes(String permission) {
        if(permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            return Pair.create(@StringRes R.string.WRITE_EXTERNAL_STORAGE_permision_message, @DrawableRes R.drawable.ic_menu_gallery);
        return null;
    }

如果这不起作用,那么只需删除两个@XRes注释

答案 1 :(得分:0)

您不能以这种方式使用这些注释。它们主要用于将参数值限制为给定类型,例如: @StringRes要求指定的int值是有效的字符串资源。

对于这个Pair,您仍然需要编写Integer,因为这是实际的变量类型:

private Pair<Integer, Integer> getRespectiveStingAndDrawableRes(String permission) {
    ...
}