"长度必须至少为1(是-1)"是什么原因导致这个问题以及如何解决呢? [英] "Length must be at least 1 (was -1)" what causes this and how do I fix it?

查看:160
本文介绍了"长度必须至少为1(是-1)"是什么原因导致这个问题以及如何解决呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这一行的code错误:

I get the error with this line of code:

这是什么的getBackground()是这样的:

public class MyClass {

  ...

  private String background;

  public String getBackground() {
    return background;
  }

  ...
}

我已经验证了 Col​​or.parseColor 需要一个字符串。为什么发生这种情况,我怎么可以让编译器高兴吗?

I've verified that Color.parseColor takes a string. Why this is happening, and how I can make the compiler happy?

这不会停止运行的应用程序,所以也许这是一个静态分析器的事情吗?我试图把来自的getBackground()的返回值在本地和检查,这是不空,不得不长度()> = 1 ,但并没有出现解决任何事情。

This doesn't stop the app from running, so perhaps this a static analyzer thing? I tried putting the return value from getBackground() in a local and checking that it was non-null and had length() >= 1, but that didn't appear to solve anything.

推荐答案

看起来pretty的很像问题与静态分析检查之一。如果你通过字符串文字到颜色#parseColor(字符串),例如: Col​​or.parseColor(#EEE); 问题消失
。 貌似静态分析是不够聪明的推断值传递的(这是返回值的getBackground())的非空在所有可能的情况,从而它表明你的可能的这个值是或空。如果您确定颜色字符串不为空的,你可以只燮preSS它像这样:

Looks pretty much like the issue with one of the static analyzer inspections. If you pass String literal to the Color#parseColor(String), e.g. Color.parseColor("#eee"); the issue disappears.
Looks like static analyzer isn't smart enough to infer that the value you pass in (which is return value of getBackground()) is non-null in all possible cases, thus it shows you that possibly this value is null or empty. If you're sure that color String is non-null you can just suppress it like so:

//noinspection ResourceType
view.setColor(Color.parseColor(model.getColor().getBackground()));

这篇关于"长度必须至少为1(是-1)"是什么原因导致这个问题以及如何解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆