Android: Java.lang.illegalargumentexception: Unknown Color
Solution 1:
From documentation:
public static int parseColor (String colorString)
Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
Your method probably returns a string that doesn't start with a #
.
Solution 2:
Wrap it inside a try catch block, and set in the catch block the default color to handle the exception. Make sure this color is right typed, also you can throw an exception to handle the failure.
For example, I'm parsing a color from Firebase remote config, if the fetch of that color throws an IllegalArgumentException
I set the color to be the default in my app.
try{
color = Color.parseColor(RemoteConfigSingleton.getInstance().getEventColor());
}catch (IllegalArgumentException e){
color = Color.parseColor("#E53935");
}
Doing this I avoid the fatal crash of the app
Solution 3:
I want to add something with the accepted answer. I had similar exception while I'm trying to use black color using Color.parseColor("#000")
which start with #
.So the thing is inside your parseColor
method it only accept a six digit hex code of color,If it is less or greater than this its give the exception.I change it like #121212
(black) this and it works for me.
Post a Comment for "Android: Java.lang.illegalargumentexception: Unknown Color"