Android: java.lang.IllegalArgumentException: Unknown color

前端 未结 3 934
小鲜肉
小鲜肉 2020-12-18 18:35

I\'m getting the above error when I try to run the following code:

int colourInt = Color.parseColor(colour.getHexValue());

The offending he

3条回答
  •  独厮守ぢ
    2020-12-18 19:24

    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

提交回复
热议问题