2016-12-19 16 views
0

アプリの起動時に背景色を変更しようとしています。ここで起動時にアプリがクラッシュします。なぜですか?

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])); 
    } 

それを起動しようとすると、このメソッドは、アプリケーションがクラッシュします。コードの背景色を変更するもう一つの良い方法はありますか?

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)  
+0

このライン 'constraintLayout.setBackgroundColo r(Color.parseColor(rColors [rColor]));は動作しません。その名前が付いた色のIDを取得するには、反射を使用する必要があります。次に、そのIDに基づいて色リソースを適切に取得します。 –

+0

@Rotwangご意見ありがとうございます。それはどのように機能するのですか?それにリンクがありますか? – Carlton

+0

名前を付けてランダムなイメージを表示するのと同じ方法です。しかし今回は '' drawIdentifier() 'の型として' 'drawable''の代わりに' 'color''を使います。もちろん、あなたは 'getResources()。getDrawable()'の代わりに 'getResources()。getColor()'を使いたいと思っています。@stackoverflow.com/a/20550354/2649012 –

答えて

1

あなたが代わりにString[]TypedArrayリソースを使用する必要があります

getRandonColors()は私がcolors.xml

EDITで定義されたいくつかの色の名前をString[]を返します。色の配列を格納します。これにより、色を参照するための自動補完とエラー検出が提供され、プロセスのエラーが発生しにくくなります。そして、あなたはActivityコード内でこの配列を参照することができ

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

:あなたはあなたのcolors.xmlファイルでこれを行う、または別のarrays.xmlファイルを作成することができます

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()); 
関連する問題