Androidstudioで簡単なジャンケンアプリを作っているのですが、CPUのimageViewだけがエミュレータを実行するといつもなぜか消えてしまいます。
初めて間もないので何が原因なのかよくわからないので、教えて頂けれと助かります。
###該当のソースコード
xml
1 <TextView 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:text="@string/txtInfoStr" 5 android:id="@+id/txtInfo" 6 android:textIsSelectable="false" 7 android:textSize="25dp" /> 8 9 <LinearLayout 10 android:orientation="horizontal" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content"> 13 14 <ImageView 15 android:layout_width="150dp" 16 android:layout_height="100dp" 17 android:id="@+id/imgGu" 18 android:src="@drawable/j_gu02" 19 android:layout_weight="1" 20 android:onClick="itemSelectAction"/> 21 22 <TextView 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_weight="1" 26 android:text="CPUの手" 27 android:id="@+id/txtCpu" 28 android:textSize="25dp" 29 android:layout_gravity="bottom" 30 android:gravity="center|center_horizontal" /> 31 32 </LinearLayout> 33 34 <LinearLayout 35 android:orientation="horizontal" 36 android:layout_width="384dp" 37 android:layout_height="wrap_content"> 38 39 <ImageView 40 android:layout_width="150dp" 41 android:layout_height="100dp" 42 android:id="@+id/imgCh" 43 android:src="@drawable/j_ch02" 44 android:layout_weight="1" 45 android:onClick="itemSelectAction"/> 46 47 48 49 <ImageView 50 android:layout_width="100dp" 51 android:layout_height="100dp" 52 android:id="@+id/imgCpu" 53 android:src="@drawable/j_gu02" 54 android:layout_weight="1" 55 android:layout_gravity="right" 56 android:onClick="startClickAction" 57 /> 58 59 </LinearLayout> 60 61 <LinearLayout 62 android:orientation="horizontal" 63 android:layout_width="374dp" 64 android:layout_height="wrap_content" 65 android:layout_gravity="center_horizontal" 66 > 67 68 <ImageView 69 android:layout_width="150dp" 70 android:layout_height="100dp" 71 android:id="@+id/imgPa" 72 android:src="@drawable/j_pa02" 73 android:layout_weight="1" 74 android:onClick="itemSelectAction"/> 75 76 <Button 77 android:layout_width="wrap_content" 78 android:layout_height="wrap_content" 79 android:layout_weight="1" 80 android:text="@string/btnStartStr" 81 android:id="@+id/btnStart" 82 android:layout_gravity="bottom|top" 83 android:textSize="20dp" 84 android:onClick="startClickAction"/> 85 86 </LinearLayout> 87 88 <LinearLayout 89 android:orientation="horizontal" 90 android:layout_width="match_parent" 91 android:layout_height="wrap_content" 92 android:layout_gravity="right"> 93 94 <Button 95 android:layout_width="wrap_content" 96 android:layout_height="wrap_content" 97 android:layout_weight="1" 98 android:text="@string/btnResetStr" 99 android:id="@+id/btnReset" 100 android:textSize="20dp" 101 android:onClick="resetClickAction" /> 102 103 </LinearLayout> 104
Java
1public class MainActivity extends AppCompatActivity { 2 3 private int selectedItem = -1; 4 5 private TextView txtInfo; 6 private Button btnStart; 7 private Button btnReset; 8 private ImageView imgCpu; 9 private ImageView[] items; 10 11 private int winCount = 0; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 txtInfo = (TextView) findViewById(R.id.txtInfo); 19 btnStart = (Button) findViewById(R.id.btnStart); 20 btnReset = (Button) findViewById(R.id.btnReset); 21 imgCpu = (ImageView) findViewById(R.id.imgCpu); 22 23 24 items = new ImageView[3]; 25 26 items[0] = (ImageView) findViewById(R.id.imgGu); 27 28 items[1] = (ImageView) findViewById(R.id.imgCh); 29 30 items[2] = (ImageView) findViewById(R.id.imgPa); 31 32 setViewStatus(0); 33 } 34 35 36 public void itemSelectAction(View view){ 37 38 for (int i = 0;i < items.length; i++){ 39 40 if (items[i] == view){ 41 42 items[i].setBackgroundColor(Color.RED); 43 44 selectedItem = i; 45 46 } else { 47 48 items[i].setBackgroundColor(Color.WHITE); 49 } 50 } 51 52 setViewStatus(1); 53 54 } 55 56 public void startClickAction(View view){ 57 int[] item = {R.drawable.j_gu02, R.drawable.j_ch02, R.drawable.j_pa02}; 58 Random rd = new Random(); 59 int cpu = rd.nextInt(3); 60 61 ImageView scr = (ImageView)findViewById(R.id.imgCpu); 62 scr.setImageResource(item[cpu]); 63 64 jadgeJanken(cpu); 65 } 66 67 68 public void resetClickAction(View view){ 69 70 setViewStatus(0); 71 72 } 73 74 private void setViewStatus(int flog){ 75 76 switch (flog){ 77 78 case 0: 79 80 txtInfo.setText("何連勝できるかな?"); 81 txtInfo.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); 82 83 for (int i = 0; i < items.length; i++){ 84 85 items[i].setBackgroundColor(Color.WHITE); 86 87 items[i].setClickable(true); 88 } 89 90 btnStart.setEnabled(false); 91 btnReset.setEnabled(false); 92 imgCpu.setVisibility(View.INVISIBLE); 93 94 break; 95 96 case 1: 97 98 btnStart.setEnabled(true); 99 break; 100 101 case 2: 102 103 for (int i = 0; i < items.length; i++){ 104 105 items[i].setClickable(false); 106 107 } 108 109 btnStart.setEnabled(false); 110 btnReset.setEnabled(true); 111 imgCpu.setVisibility(View.VISIBLE); 112 113 break; 114 } 115 } 116 117 118 private void jadgeJanken(int cpu){ 119 120 txtInfo.setTextColor(ContextCompat.getColor(this, R.color.colorAccent)); 121 122 123 if (selectedItem == cpu){ 124 txtInfo.setText("引き分け"); 125 winCount = 0; 126 } else if ((selectedItem == 0 && cpu == 1) || 127 (selectedItem == 1 && cpu == 2) || 128 (selectedItem == 2 && cpu == 0)) { 129 winCount++; 130 txtInfo.setText("あなたの勝ち" + winCount + "連勝中!"); 131 132 } else { 133 txtInfo.setText("あなたの負け"); 134 winCount = 0; 135 } 136 } 137 138}
ImageViewではわかりません。idがあるならを書いてください。