Android: java.lang.IllegalArgumentException: Unknown color

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 #.


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

Tags:

Colors

Android