【问题标题】:using ternary for returning a color resource with annotaion @ColorInt android使用三元返回带有注释@ColorInt android的颜色资源
【发布时间】:2016-12-10 11:04:47
【问题描述】:

我有两个相同功能的代码。


代码 1

@ColorInt
int getColor(@ColorRes int id) {
    if (getActivity() == null) {
        return Color.BLACK;
    } else {
        return ContextCompat.getColor(getActivity(), id);
    }
}

代码 2

@ColorInt
int getColor(@ColorRes int id) {
    return getActivity()==null?Color.BLACK:ContextCompat.getColor(getActivity(), id);
}

第一个代码 sn-p 没有给出错误,但第二个代码给出了一个错误 应该传递解析的颜色而不是资源 id 并且当我删除 @ColorInt 注释第二个代码工作正常,有人可以帮忙解释为什么会这样。

【问题讨论】:

    标签: java android annotations ternary-operator


    【解决方案1】:

    ContextCompat.getColor 方法未使用@ColorInt 进行注释。 所以 Lint 工具会出现这个错误。 HereContextCompat.getColor的源代码。

    Lint 工具在下面的代码中检查方法的注释。

    Here 是相同的错误报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2016-12-10
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多