String colorString = myObject.getColor(); // "0000ff"
colorString = colorString == null ? "#44FF0000" : "#44" + colorString;
int myColor = Color.parseColor(colorString); // Length must be at least 1
来自android.graphics
库的 Color.parseColor(String colorString)
使用android.annotation.Size
中的@Size(min=1)
进行注释。为什么编译器认为上面的三元操作的结果字符串可能为空,而下面的代码有效?
String colorString = myObject.getColor(); // "0000ff"
if (colorString == null) {
colorString = "44FF0000";
} else {
colorString = "#44" + colorString;
}
int myColor = Color.parseColor(colorString); // all's good