【问题标题】:java.lang.IllegalArgumentException: Unknown color on use static color stringjava.lang.IllegalArgumentException:使用静态颜色字符串时的颜色未知
【发布时间】:2018-09-20 14:01:30
【问题描述】:

创建这个类之后

public class MyColors {
public static String COLOR_APPLICATION = "#FF445FC7";
public static String COLOR_APPLICATION_OPACITY = "#cbb19155";
public static String PRIMARY_COLOR = "#211d1c";
public static String NAVBAR_COLOR = "#0c0c0c";
public static String BACKGROUND_COLOR = "#cbb19155";}

然后创建:

public class MyDrawables {

public static Drawable txt_shape = new DrawableBuilder()
        .rectangle().bottomLeftRadius(9).bottomRightRadius(9)
        .solidColor(Color.parseColor(MyColors.COLOR_APPLICATION))
        .build();

public static Drawable text_view_center_shape = new DrawableBuilder()
        .rectangle().solidColor(Color.parseColor("#009c3bbc"))
        .strokeColor(Color.parseColor("#9f9f9f")).strokeWidth(1)
        .build();}

gettig 使用 myDrawable 时出现的异常,例如:

serviceTxt.setBackground(MyDrawables.txt_shape);

我能做些什么来避免这种崩溃?

P.S:在活动中创建我的可绘制对象并使用它时,应用程序不会崩溃。

【问题讨论】:

  • 请发布例外情况。
  • java.lang.ExceptionInInitializerError: java.lang.IllegalArgumentException: Unknown color @kelalaka
  • 你能用你的字符串用小应用程序测试吗? stackoverflow.com/questions/8225185/…

标签: android android-drawable


【解决方案1】:

使用setBackgroundDrawable() 而不是setBackground() 如果您想将颜色设置为背景,请使用setBackgroundColor()

【讨论】:

    【解决方案2】:

    您可以使用静态方法,例如:

        public static Drawable txtShape (){
        return new DrawableBuilder()
        .rectangle().bottomLeftRadius(9).bottomRightRadius(9)
        .solidColor(Color.parseColor(MyColors.COLOR_APPLICATION))
        .build();}
    

    或者在 res 目录中的 drawable 文件夹中创建您需要的所有形状,如 .xml 文件,并像 R.drawable.text_shape 一样引用它们。

    最好将颜色存储在 res/values/colors.xml

    【讨论】:

    • 还是不行,我不会在drawable中创建,因为颜色在应用程序中是动态的
    【解决方案3】:

    对此不太确定,但请重新检查您的颜色字​​符串格式。 如果我没记错的话,颜色字符串是指颜色十六进制代码,即“#xxxxxx”

    public class MyColors {
    public static String COLOR_APPLICATION = "#FF445FC7"; //<= recheck this
    public static String COLOR_APPLICATION_OPACITY = "#cbb19155"; //<= recheck this
    public static String PRIMARY_COLOR = "#211d1c"; //<= this is the right format
    public static String NAVBAR_COLOR = "#0c0c0c";
    public static String BACKGROUND_COLOR = "#cbb19155";} //<= recheck this
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2012-01-03
      • 2017-10-22
      • 1970-01-01
      • 2012-10-07
      相关资源
      最近更新 更多