Android java.lang.IllegalArgumentException:使用静态颜色字符串时颜色未知

Android java.lang.IllegalArgumentException:使用静态颜色字符串时颜色未知,android,android-drawable,Android,Android Drawable,创建这个类之后 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

创建这个类之后

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();}
在使用myDrawable时获取该异常,如:

serviceTxt.setBackground(MyDrawables.txt_shape);
我能做些什么来避免这次撞车


注意:在创建“我的可绘制”活动并使用它时,应用程序不会崩溃。

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

    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\u shape一样引用它们

最好将颜色存储在res/values/colors.xml中,使用setBackgroundDrawable而不是setBackground
如果您想设置一种颜色作为背景,请使用setBackgroundColor。

对此不是很确定,但请重新检查您的颜色字符串格式。 如果我没弄错,颜色字符串指的是颜色十六进制代码,即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

请发布exception.java.lang.ExceptionInInitializerError:java.lang.IllegalArgumentException:Unknown color@Kellalakacan您能用字符串测试小型应用程序吗?仍然不工作,我不会在drawable中创建,因为颜色在应用程序中是动态的