Reputation: 3016
This is strange, I have used this function before. however it seems to have stopped working and I am not sure why.
I use Color.parseColor
to set a background color of an item.
This is the code doing it:
imgItemImage.setBackgroundColor(Color.parseColor(cartItem.get("picture")));
I am getting this exception:
java.lang.NumberFormatException: For input string: "#837A67"
at java.lang.Long.parseLong(Long.java:579)
at android.graphics.Color.parseColor(Color.java:1386)
That appears to be a correct hex color so I am unsure why it would be causing this exception.
Does anyone have any idea?
Upvotes: 1
Views: 903
Reputation: 4667
As you can see from the Exception
, the problem is when Long.parseLong()
is being called from inside Color.parseColor()
.
This Exception
is due to wrong input being given to Long.parseLong() which is an extra character of #
, which should normally be removed by Color.parseColor()
before internally calling Long.parseLong()
.
The Exception
thrown by your code can demonstrated below:
String str = "#837A67";
System.out.println(Long.parseLong(str, 16));
Where 16
is the RADIX for hexadecimal.
But try this:
String str = "837A67";
System.out.println(Long.parseLong(str, 16));
And you will get the expected results.
This shows that the String
getting passed through parseColor
is not correct because the #
is not getting removed during Color.parseColor()
most likely due to a different character getting removed instead.
Double check into the value you are passing to Color.parseColor()
and ensure there are not any characters before the #
.
Upvotes: 3