应用程序在启动时崩溃了,为什么?

时间:2016-12-19 18:30:24

标签: android

我正在尝试在应用程序启动时更改背景颜色。

以下是从onCreate()调用的方法:

private void setBackground() {
        String[] rColors = getRandomColors();
        int rColor = new Random().nextInt(rColors.length);
        ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
        constraintLayout.setBackgroundColor(Color.parseColor(rColors[rColor]));
    }

此方法在尝试启动时会崩溃应用程序。还有另一种更好的方法来改变代码中的背景颜色吗?

getRandonColors()返回String[],其中包含我在colors.xml

中定义的某些颜色的名称

编辑:

String[] rColors = getRandomColors();
int rColor = new Random().nextInt(rColors.length);
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
int i = MainActivityNew.this.getResources().getIdentifier(rColors[rColor],"color",MainActivityNew.this.getPackageName());
constraintLayout.setBackgroundColor(i);

EDIT2:

FATAL EXCEPTION: main
                                                   Process: com.asus.wetr, PID: 12941
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asus.wetr/com.asus.wetr.activities.MainActivityNew}: java.lang.IllegalArgumentException: Unknown color
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.IllegalArgumentException: Unknown color
                                                       at android.graphics.Color.parseColor(Color.java:235)
                                                       at com.asus.wetr.activities.MainActivityNew.setBackground(MainActivityNew.java:214)
                                                       at com.asus.wetr.activities.MainActivityNew.setUpUIComponents(MainActivityNew.java:143)
                                                       at com.asus.wetr.activities.MainActivityNew.onCreate(MainActivityNew.java:111)
                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:1)

您应该使用TypedArray资源而不是String[]来存储颜色数组。这将为您的颜色参考提供自动完成和错误检测,使该过程不易出错。您可以在colors.xml文件中执行此操作,也可以创建单独的arrays.xml文件:

<resources>

    <array name="colors">
        <item>@color/red</item>
        <item>@color/orange</item>
        <item>@color/yellow</item>
        <item>@color/green</item>
        <item>@color/blue</item>
        <item>@color/indigo</item>
        <item>@color/violet</item>
    </array>

</resources>

然后,您可以在Activity代码中引用此数组:

private int getRandomColor() {
    TypedArray a = getResources().obtainTypedArray(R.array.colors);

    int random = new Random().nextInt(a.length());
    int defValue = ContextCompat.getColor(this, R.color.red);
    int color = a.getColor(random, defValue);

    a.recycle();

    return color;
}

然后你可以调用它来设置整个Window(或者你喜欢的特定View)的背景颜色:

getWindow().getDecorView().setBackgroundColor(getRandomColor());