質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

1回答

1352閲覧

Android StudioのimageViewについて

hatch_1564

総合スコア8

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/12/15 02:26

編集2016/12/15 02:28

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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yona

2016/12/15 06:18

ImageViewではわかりません。idがあるならを書いてください。
guest

回答1

0

んー、コード長いし問題だらけですね

java

1private ImageView imgCpu; 2 3mgCpu = (ImageView) findViewById(R.id.imgCpu); 4 5imgCpu.setVisibility(View.INVISIBLE); 6 7ImageView scr = (ImageView)findViewById(R.id.imgCpu) 8

imgCpuというIDからImageViewのインスタンスを作っておきながら、
また同じリソースで別のインスタンスを生成している
imgCpuはINVISIBLEしたままなので見えない

また、リソースのIDであるimgCpuとインスタンスimgCpuが同じなのはバグの元ですからやめましょう

何が原因なのか

あなたの中で整理されていないのが原因でしょう
どこかからコピペして組み合わせただけとか
コードの記述が先走りして、途中で確認を怠ったために
どこまでが確実にできているかわからなくなっているのではありませんか?

とりあえず

java

1 public void startClickAction(View view){ 2... 3 //ImageView scr = (ImageView)findViewById(R.id.imgCpu); 4 //scr.setImageResource(item[cpu]); 5 6 imgCpu.setVisibility(View.VISIBLE); 7 imgCpu.setImageResource(item[cpu]); 8... 9 } 10

ImageViewは表示されるとは思います
他にも問題がありますが、もう一度アプリの構成を確認しましょう

投稿2016/12/15 07:39

aja

総合スコア3733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問