gat~gat6までのImageViewに、それぞれRandomクラスで出た変数rの数に応じて、4つある配列のうち1つの配列を選び、
その配列のうち1枚の画像を表示する、という事を6回繰り返すプログラムを組もうとしています
しかし『java.lang.Iterable の配列またはインスタンスのみ繰り返すことができます』というエラーが出てしまい、
それを消すには何というコードを入れれば良いのでしょうか
また、意図した通りにコードを動かすにあたって他にコードの間違いがありましたらご指摘をお願いします
lang
1package jp.example.wgamen; 2 3import java.util.Random; 4 5import android.app.Activity; 6import android.content.Intent; 7import android.content.res.TypedArray; 8import android.os.Bundle; 9import android.view.View; 10import android.widget.Button; 11import android.widget.ImageView; 12 13public class SubActivity extends Activity { 14 15 ImageView gat; 16 ImageView gat2; 17 ImageView gat3; 18 ImageView gat4; 19 ImageView gat5; 20 ImageView gat6; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.sub); 26 27 // 画面に配置したImageViewを設定 28 gat = (ImageView)findViewById(R.id.v1); 29 gat2 = (ImageView)findViewById(R.id.v2); 30 gat3 = (ImageView)findViewById(R.id.v3); 31 gat4 = (ImageView)findViewById(R.id.v4); 32 gat5 = (ImageView)findViewById(R.id.v5); 33 gat6 = (ImageView)findViewById(R.id.v6); 34 35 //整数を入れる為の変数を宣言 36 int gatya2; 37 38 39 // 画像のリストを取得 40 TypedArray images = getResources().obtainTypedArray(R.array.r); 41 TypedArray images2 = getResources().obtainTypedArray(R.array.sr); 42 TypedArray images3 = getResources().obtainTypedArray(R.array.ur); 43 TypedArray images4 = getResources().obtainTypedArray(R.array.lr); 44 45 // Randomクラスのインスタンスを生成 46 Random r = new Random(); 47 48 //0以上100未満の整数の乱数を作成する 49 gatya2 = r.nextInt(100); 50 51 //ランダムで出た数が2以下ならLRの配列から1枚画像を表示する 52 if(gatya2 <= 2){ 53 gat.setImageResource(R.array.lr); 54 55}else 56 //ランダムで出た数が3以上12以下ならURの配列から1枚画像を表示する 57 if(gatya2 <= 12){ 58 gat.setImageResource(R.array.ur); 59}else 60 //ランダムで出た数が13以上39以下ならSRの配列から1枚画像を表示する 61 if(gatya2 <= 39){ 62 gat.setImageResource(R.array.sr); 63}else{ 64 //それ以外(それ以降)の数字ならRの配列から1枚画像を表示する 65 gat.setImageResource(R.array.r); 66} 67 68//2~5まで繰り返しなので省略・ 69 70 //ランダムで出た数が2以下ならLRの配列から1枚画像を表示する 71 if(gatya2 <= 2){ 72 gat6.setImageResource(R.array.lr); 73}else 74 //ランダムで出た数が3以上12以下ならURの配列から1枚画像を表示する 75 if(gatya2 <= 12){ 76 gat6.setImageResource(R.array.ur); 77}else 78 //ランダムで出た数が13以上39以下ならSRの配列から1枚画像を表示する 79 if(gatya2 <= 39){ 80 gat6.setImageResource(R.array.sr); 81}else{ 82 //それ以外(それ以降)の数字ならRの配列から1枚画像を表示する 83 gat6.setImageResource(R.array.r); 84} 85 86 // 各ImageViewに画像を設定 ↓ここで?の部分に何を入れればいいか分からずエラーが出ます 87 for(ImageView tmp : ?) 88 { 89 tmp.setImageDrawable(images.getDrawable(r.nextInt(images.length()))); 90 tmp.setImageDrawable(images2.getDrawable(r.nextInt(images2.length()))); 91 tmp.setImageDrawable(images3.getDrawable(r.nextInt(images3.length()))); 92 tmp.setImageDrawable(images4.getDrawable(r.nextInt(images4.length()))); 93 } 94
よろしくお願いします
追記:回答下さった皆様のおかげで、今こういうコードになっています
lang
1package jp.example.wgamen; 2 3import java.util.Arrays; 4import java.util.Random; 5 6import android.annotation.SuppressLint; 7import android.app.Activity; 8import android.content.Intent; 9import android.content.res.TypedArray; 10import android.os.Bundle; 11import android.view.View; 12import android.widget.Button; 13import android.widget.ImageView; 14 15public class SubActivity extends Activity { 16 17 ImageView gat; 18 ImageView gat2; 19 ImageView gat3; 20 ImageView gat4; 21 ImageView gat5; 22 ImageView gat6; 23 24 @SuppressLint("Recycle") 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.sub); 29 30 // 画面に配置したImageViewを設定 31 gat = (ImageView)findViewById(R.id.v1); 32 gat2 = (ImageView)findViewById(R.id.v2); 33 gat3 = (ImageView)findViewById(R.id.v3); 34 gat4 = (ImageView)findViewById(R.id.v4); 35 gat5 = (ImageView)findViewById(R.id.v5); 36 gat6 = (ImageView)findViewById(R.id.v6); 37 38 //整数を入れる為の変数を宣言 39 int gatya2; 40 41 42 // 画像のリストを取得 43 TypedArray images = getResources().obtainTypedArray(R.array.r); 44 TypedArray images2 = getResources().obtainTypedArray(R.array.sr); 45 TypedArray images3 = getResources().obtainTypedArray(R.array.ur); 46 TypedArray images4 = getResources().obtainTypedArray(R.array.lr); 47 48 // Randomクラスのインスタンスを生成 49 Random r = new Random(); 50 51 for(ImageView tmp : Arrays.asList(gat, gat2, gat3, gat4, gat5, gat6)) { 52 //0以上100未満の整数の乱数を作成する 53 gatya2 = r.nextInt(100); 54 //ランダムで出た数が2以下ならLRの配列から1枚画像を表示する 55 if(gatya2 <= 2){ 56 gat.setImageDrawable(images4.getDrawable(r.nextInt(images4.length()))); 57 //ランダムで出た数が3以上12以下ならURの配列から1枚画像を表示する 58 }else if(gatya2 <= 12){ 59 gat.setImageDrawable(images3.getDrawable(r.nextInt(images3.length()))); 60 //ランダムで出た数が13以上39以下ならSRの配列から1枚画像を表示する 61 }else if(gatya2 <= 39){ 62 gat.setImageDrawable(images2.getDrawable(r.nextInt(images2.length()))); 63 //それ以外(それ以降)の数字ならRの配列から1枚画像を表示する 64 }else{ 65 gat.setImageDrawable(images.getDrawable(r.nextInt(images.length()))); 66 } 67//gat2~gat5まで同じなので省略 68 if(gatya2 <= 2){ 69 gat6.setImageDrawable(images4.getDrawable(r.nextInt(images4.length()))); 70 }else if(gatya2 <= 12){ 71 gat6.setImageDrawable(images3.getDrawable(r.nextInt(images3.length()))); 72 }else if(gatya2 <= 39){ 73 gat6.setImageDrawable(images2.getDrawable(r.nextInt(images2.length()))); 74 }else{ 75 gat6.setImageDrawable(images.getDrawable(r.nextInt(images.length()))); 76 } 77 } 78
かなり完成度が高いコードになったと思うのですが、何故か強制終了してしまいます
何度もコードを読み返しているのですが、何が原因なのか分かりません
気になる所といえば、for(ImageView tmp : のtmpに『ローカル変数tmpの値は使用されていません』という警告が出ている事くらいです
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/06/19 14:03
2015/06/19 14:06
2015/06/19 14:25
2015/06/19 14:40
2015/06/19 14:42
2015/06/19 14:46
2015/06/19 14:58
2015/06/19 15:06
2015/06/19 15:29
2015/06/19 15:34
2015/06/19 16:16
2015/06/20 11:31
2015/06/20 11:55
2015/06/20 12:04
2015/06/20 13:04
2015/06/20 13:16
2015/06/20 13:39