質問編集履歴

2

誤字

2015/06/19 14:00

投稿

mari_tom
mari_tom

スコア12

test CHANGED
File without changes
test CHANGED
@@ -238,8 +238,6 @@
238
238
 
239
239
  public class SubActivity extends Activity {
240
240
 
241
- private Button btn;
242
-
243
241
 
244
242
 
245
243
  ImageView gat;

1

追記

2015/06/19 14:00

投稿

mari_tom
mari_tom

スコア12

test CHANGED
File without changes
test CHANGED
@@ -201,3 +201,175 @@
201
201
  ```
202
202
 
203
203
  よろしくお願いします
204
+
205
+
206
+
207
+ 追記:回答下さった皆様のおかげで、今こういうコードになっています
208
+
209
+ ```lang-<Java>
210
+
211
+ package jp.example.wgamen;
212
+
213
+
214
+
215
+ import java.util.Arrays;
216
+
217
+ import java.util.Random;
218
+
219
+
220
+
221
+ import android.annotation.SuppressLint;
222
+
223
+ import android.app.Activity;
224
+
225
+ import android.content.Intent;
226
+
227
+ import android.content.res.TypedArray;
228
+
229
+ import android.os.Bundle;
230
+
231
+ import android.view.View;
232
+
233
+ import android.widget.Button;
234
+
235
+ import android.widget.ImageView;
236
+
237
+
238
+
239
+ public class SubActivity extends Activity {
240
+
241
+ private Button btn;
242
+
243
+
244
+
245
+ ImageView gat;
246
+
247
+ ImageView gat2;
248
+
249
+ ImageView gat3;
250
+
251
+ ImageView gat4;
252
+
253
+ ImageView gat5;
254
+
255
+ ImageView gat6;
256
+
257
+
258
+
259
+ @SuppressLint("Recycle")
260
+
261
+ @Override
262
+
263
+ protected void onCreate(Bundle savedInstanceState) {
264
+
265
+ super.onCreate(savedInstanceState);
266
+
267
+ setContentView(R.layout.sub);
268
+
269
+
270
+
271
+ // 画面に配置したImageViewを設定
272
+
273
+ gat = (ImageView)findViewById(R.id.v1);
274
+
275
+ gat2 = (ImageView)findViewById(R.id.v2);
276
+
277
+ gat3 = (ImageView)findViewById(R.id.v3);
278
+
279
+ gat4 = (ImageView)findViewById(R.id.v4);
280
+
281
+ gat5 = (ImageView)findViewById(R.id.v5);
282
+
283
+ gat6 = (ImageView)findViewById(R.id.v6);
284
+
285
+
286
+
287
+ //整数を入れる為の変数を宣言
288
+
289
+ int gatya2;
290
+
291
+
292
+
293
+
294
+
295
+ // 画像のリストを取得
296
+
297
+ TypedArray images = getResources().obtainTypedArray(R.array.r);
298
+
299
+ TypedArray images2 = getResources().obtainTypedArray(R.array.sr);
300
+
301
+ TypedArray images3 = getResources().obtainTypedArray(R.array.ur);
302
+
303
+ TypedArray images4 = getResources().obtainTypedArray(R.array.lr);
304
+
305
+
306
+
307
+ // Randomクラスのインスタンスを生成
308
+
309
+ Random r = new Random();
310
+
311
+
312
+
313
+ for(ImageView tmp : Arrays.asList(gat, gat2, gat3, gat4, gat5, gat6)) {
314
+
315
+ //0以上100未満の整数の乱数を作成する
316
+
317
+ gatya2 = r.nextInt(100);
318
+
319
+ //ランダムで出た数が2以下ならLRの配列から1枚画像を表示する
320
+
321
+ if(gatya2 <= 2){
322
+
323
+ gat.setImageDrawable(images4.getDrawable(r.nextInt(images4.length())));
324
+
325
+ //ランダムで出た数が3以上12以下ならURの配列から1枚画像を表示する
326
+
327
+ }else if(gatya2 <= 12){
328
+
329
+ gat.setImageDrawable(images3.getDrawable(r.nextInt(images3.length())));
330
+
331
+ //ランダムで出た数が13以上39以下ならSRの配列から1枚画像を表示する
332
+
333
+ }else if(gatya2 <= 39){
334
+
335
+ gat.setImageDrawable(images2.getDrawable(r.nextInt(images2.length())));
336
+
337
+ //それ以外(それ以降)の数字ならRの配列から1枚画像を表示する
338
+
339
+ }else{
340
+
341
+ gat.setImageDrawable(images.getDrawable(r.nextInt(images.length())));
342
+
343
+ }
344
+
345
+ //gat2~gat5まで同じなので省略
346
+
347
+ if(gatya2 <= 2){
348
+
349
+ gat6.setImageDrawable(images4.getDrawable(r.nextInt(images4.length())));
350
+
351
+ }else if(gatya2 <= 12){
352
+
353
+ gat6.setImageDrawable(images3.getDrawable(r.nextInt(images3.length())));
354
+
355
+ }else if(gatya2 <= 39){
356
+
357
+ gat6.setImageDrawable(images2.getDrawable(r.nextInt(images2.length())));
358
+
359
+ }else{
360
+
361
+ gat6.setImageDrawable(images.getDrawable(r.nextInt(images.length())));
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+ ```
370
+
371
+ かなり完成度が高いコードになったと思うのですが、何故か強制終了してしまいます
372
+
373
+ 何度もコードを読み返しているのですが、何が原因なのか分かりません
374
+
375
+ 気になる所といえば、for(ImageView tmp : のtmpに『ローカル変数tmpの値は使用されていません』という警告が出ている事くらいです