RadioButtonに関してエラーが起きました。
java
1コードpackage com.example.mikawa.myapplication; 2import android.content.Intent; 3import android.database.Cursor; 4import android.database.sqlite.SQLiteDatabase; 5import android.support.v7.app.AppCompatActivity; 6import android.os.Bundle; 7import android.view.View; 8import android.view.Window; 9import android.widget.Button; 10import android.widget.RadioButton; 11import android.widget.TextView; 12 13/** 14 * Created by mikawa on 2016/11/18. 15 */ 16 17class Global{ 18 public static int count = 1; 19} 20 21public class QuestionActivity extends AppCompatActivity implements View.OnClickListener { 22 23 24 String Seikai; 25 String QuestionNO; 26 private Button Button4; 27 int[] Ans_QA; 28 private int Count = 0; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 //タイトルバーを非表示にする 34 requestWindowFeature(Window.FEATURE_NO_TITLE); 35 //レイアウトをセットする 36 setContentView(R.layout.page2_change); 37 38 Button4 = (Button) findViewById(R.id.button4); 39 Button4.setOnClickListener(this); 40 41 42 43 //SubActivityから送られてきたデータを取得 44 Intent intent = getIntent(); 45 Ans_QA = intent.getIntArrayExtra("Answer"); 46 47 /* //データを取得する(データがない場合、第2引数の0が返る) 48 Count = intent.getIntExtra("QuestionCo",0); 49 50 51 QuestionNO = String.valueOf(getrandom[Count - 1]);*/ 52 53 } 54 55 @Override 56 protected void onResume() { 57 super.onResume(); 58 59 //画面↑にあるテキストを「問題 + 問題No」で表示 60 ((TextView)findViewById(R.id.textNo)).setText("問題" + Global.count); 61 62 63 //ボタン処理呼び出し 64 setQuestion(); 65 } 66 67 //ボタンをクリア済みかどうかで色分け&クリック不可処理 68 private void setQuestion() { 69 //作成したDBOpenHelperクラスに読み取り専用でアクセス 70 DBOpenHelper dbOpenHelper = new DBOpenHelper(this); 71 SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); 72 73 74 /* 75 SELECT文 76 テーブル名 MyTableからClearの項目を検索してくる条件式 77 */ 78 String sql = "SELECT Pref,Q0,Q1,Q2,Q3 FROM MyTable WHERE _id= " + Global.count; 79 80 //上記のSELECT文を実行してカーソルを取得 81 Cursor c = db.rawQuery(sql, null); 82 c.moveToFirst(); 83 84 //データベースから取ってきたデータを変数にセット 85 String teniss = c.getString(c.getColumnIndex("Pref"));//問題文となる都道府県 86 String choice1 = c.getString(c.getColumnIndex("Q0"));//三択の選択肢1 87 String choice2 = c.getString(c.getColumnIndex("Q1"));//三択の選択肢2 88 String choice3 = c.getString(c.getColumnIndex("Q2"));//三択の選択肢3 89 90 Seikai = c.getString(c.getColumnIndex("Q3"));//答え 91 92 //データベースのクローズ処理 93 c.close(); 94 db.close(); 95 96 ((TextView) findViewById(R.id.textQuestion)).setText(teniss);//問題文をテキストに表示 97 ((RadioButton) findViewById(R.id.RadioButton1)).setText(choice1);//三択の選択肢1をボタンに表示 98 ((RadioButton) findViewById(R.id.RadioButton2)).setText(choice2);//三択の選択肢2をボタンに表示 99 ((RadioButton) findViewById(R.id.RadioButton3)).setText(choice3);//三択の選択肢3をボタンに表示 100 if(Global.count == 3){ 101 Button4 = ((Button) findViewById(R.id.button4)); 102 Button4.setText("解答画面へ"); 103 } 104 105 } 106 107 //選択肢がクリックされた時の処理 108 public void onClick(View v){ 109 if(((RadioButton) v).getText().equals(Seikai)){ 110 //正解の処理(正解なら”1”) 111 Ans_QA[Global.count] = 1; 112 113 }else{ 114 //不正解の処理(不正解なら”0”) 115 Ans_QA[Global.count] = 0; 116 } 117 if(v==Button4) { 118 if(Global.count != 3) { 119 //遷移先のActivityを指定 120 // Intent Intent = new Intent(このクラスから、このクラスへ) 121 Intent intent = new Intent(QuestionActivity.this, QuestionActivity.class); 122 Global.count++; 123 //Count++; 124 //intent.putExtra("QuestionCo", Count); 125 //遷移開始 126 startActivity(intent); 127 }else{ 128 Intent intent = new Intent(QuestionActivity.this, AnswerActivity.class); 129 //遷移開始 130 startActivity(intent); 131 } 132 133 } 134 135 136 } 137} 138
xml
1コード<?xml version="1.0" encoding="utf-8"?> 2 3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:gravity="left" 9 android:id="@+id/page2_change"> 10 11 12 <TextView 13 android:layout_width="match_parent" 14 android:layout_height="25dp" 15 android:textAppearance="?android:attr/textAppearanceMedium" 16 android:id="@+id/textNo" 17 android:text="Medium Text" 18 android:bufferType="editable" 19 android:layout_gravity="center_horizontal" 20 android:layout_weight="0.34" 21 android:background="#58BE89" 22 android:textColor="#FFFFFF" 23 android:textSize="26dp" /> 24 25 <TextView 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:textAppearance="?android:attr/textAppearanceMedium" 29 android:text="Medium Text" 30 android:id="@+id/textQuestion" 31 android:layout_weight="1" 32 android:gravity="center_vertical|center_horizontal" 33 android:textSize="46dp" /> 34 35 36 <RadioGroup android:layout_height="wrap_content" 37 android:layout_width="wrap_content" 38 android:id="@+id/RadioGroup"> 39 <RadioButton android:text="1" 40 android:id="@+id/RadioButton1" 41 android:layout_height="wrap_content" 42 android:layout_width="wrap_content"> 43 </RadioButton> 44 <RadioButton android:text="2" 45 android:id="@+id/RadioButton2" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content"> 48 </RadioButton> 49 <RadioButton android:text="3" 50 android:id="@+id/RadioButton3" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content"> 53 </RadioButton> 54 </RadioGroup> 55 56 <Button 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:text="次の問題へ" 60 android:bufferType="normal" 61 android:id="@+id/button4" 62 android:layout_gravity="right" 63 android:layout_marginTop="88dp"> 64 65 </Button> 66 67 68 69</LinearLayout>
logcat
1コードjava.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton 2 at com.example.mikawa.myapplication.QuestionActivity.onClick(QuestionActivity.java:109) 3```教えていただけると幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/19 14:48
2016/12/20 10:28
2016/12/20 10:43
2016/12/20 12:02