###前提・実現したいこと
Button nextをタップしてImageview quizにランダムに表示したクイズの画像をタップしたときに答えの画像に変更するアプリを作りたいのですが、下記のソースのonClick(View v)のcase R.id.quizのifとelse ifを反映させるにはどこを直したらいいでしょうか?
###発生している問題・エラーメッセージ
onClick(View v)のcase R.id.quizのifとelse ifが反映されず、どの画像をタップしてもelseになってしまいます
JAVA
1import android.app.Activity; 2import android.content.res.TypedArray; 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.graphics.drawable.Drawable; 6import android.os.Bundle; 7import android.view.View; 8import android.widget.Button; 9import android.widget.ImageView; 10 11public class MainActivity extends Activity implements View.OnClickListener { 12 13 Button next; 14 ImageView quiz; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 next = (Button) findViewById(R.id.next); 22 quiz = (ImageView)findViewById(R.id.quiz); 23 24 next.setOnClickListener(this); 25 quiz.setOnClickListener(this); 26 27 TypedArray typedArray = getResources().obtainTypedArray(R.array.question); 28 int rand = (int) (Math.floor(Math.random() * (3 - 1)) + 1); 29 Drawable drawable = typedArray.getDrawable(rand); 30 quiz.setImageDrawable(drawable); 31 } 32 33 public void onClick(View v) { 34 switch(v.getId()){ 35 36 case R.id.next: 37 38 TypedArray typedArray = getResources().obtainTypedArray(R.array.question); 39 int rand = (int) (Math.floor(Math.random() * (3))); 40 Drawable drawable = typedArray.getDrawable(rand); 41 quiz.setImageDrawable(drawable); 42 break; 43 44 case R.id.quiz: 45 int imageID = R.drawable.question00; 46 47 if (imageID == R.drawable.question01) { 48 imageID = R.drawable.answer01; 49 50 } else if (imageID == R.drawable.queation02) { 51 imageID = R.drawable.answer02; 52 53 } else { 54 imageID = R.drawable.answer00; 55 56 } 57 58 Bitmap bmp = BitmapFactory.decodeResource(getResources(), imageID); 59 quiz.setImageBitmap(bmp); 60 quiz.setImageResource(imageID); 61 break; 62 } 63 } 64}
###試したこと
int imageID = ...が間違っていると思い、public class直下に移してint imageID;の形にしてみたのですが結果は変わりませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
Android Studio 2.1.3を使用してアプリを作っています。
Buttonをタップするとランダムでクイズ画像が変わります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/18 12:08
2016/08/18 12:10
2016/08/18 13:27
2016/08/18 14:03
2016/08/18 14:15
2016/08/18 14:24
2016/08/18 15:21