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

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

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

HTMLで用いる<button>タグです。

Android Studio

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

Q&A

解決済

2回答

2859閲覧

Android studio RadioButtonのエラー

KOOC

総合スコア11

button

HTMLで用いる<button>タグです。

Android Studio

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

1グッド

1クリップ

投稿2016/12/19 10:20

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```教えていただけると幸いです。
退会済みユーザー👍を押しています

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

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

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

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

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

guest

回答2

0

ベストアンサー

idがbutton4のViewはRadioButtonではなくButtonです。キャストはできません。
まず、button4と他のRadioButtonをif文を使って判定し、それからキャストしましょう。

投稿2016/12/19 12:44

yona

総合スコア18155

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

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

KOOC

2016/12/19 14:48

yona様ご指摘ありがとうございます。早速明日試してみます。
KOOC

2016/12/20 10:28

yona様、以下のコードにしたのですがRadioButtonの処理はされませんでした。改善点をおしえていただると幸いです。 if(v==RadioButton1) { Global.b++; if (((RadioButton) v).getText().equals(Seikai)) { //正解の処理(正解なら”1”) Ans_Judge[Global.count - 1] = 1; } else { //不正解の処理(不正解なら”0”) Ans_Judge[Global.count-1] = 0; } }else if(v==RadioButton2) { if (((RadioButton) v).getText().equals(Seikai)) { //正解の処理(正解なら”1”) Ans_Judge[Global.count-1] = 1; } else { //不正解の処理(不正解なら”0”) Ans_Judge[Global.count-1] = 0; } }else if(v==RadioButton3) { if (((RadioButton) v).getText().equals(Seikai)) { //正解の処理(正解なら”1”) Ans_Judge[Global.count-1] = 1; } else{ //不正解の処理(不正解なら”0”) Ans_Judge[Global.count-1] = 0; } } else if(v==Button4) { if(Global.count != 5) { //遷移先のActivityを指定 // Intent Intent = new Intent(このクラスから、このクラスへ) Intent intent = new Intent(QuestionActivity.this, QuestionActivity.class); Global.count++; Global.Q_count++; //Count++; //intent.putExtra("QuestionCo", Count); //遷移開始 startActivity(intent); }else{ Intent intent = new Intent(QuestionActivity.this, AnswerActivity.class); intent.putExtra("AnswerJudge", Ans_Judge); intent.putExtra("AnswerCount", Global.count); intent.putExtra("AnswerQ_Count", Global.Q_count); intent.putExtra("B",Global.b); //遷移開始 startActivity(intent); } }
KOOC

2016/12/20 10:43

yona様、これを載せるのを忘れていました。 RadioButton1 = (RadioButton) findViewById(R.id.RadioButton1); RadioButton1.setText(choice1);//三択の選択肢1をボタンに表示 RadioButton2 = (RadioButton) findViewById(R.id.RadioButton2); RadioButton2.setText(choice2);//三択の選択肢2をボタンに表示 RadioButton3 = (RadioButton) findViewById(R.id.RadioButton3); RadioButton3.setText(choice3);//三択の選択肢3をボタンに表示
yona

2016/12/20 12:02

おそらくコードが間違っているからです。 まずButtonかどうかを判定して、elseブロックでRadioButtonの処理をしてください。
guest

0

button4が押されたとき、
onClickのifでRadioButtonにキャストされているのでエラーが出ているのではないでしょうか。

投稿2016/12/19 11:06

aglkjggg

総合スコア769

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

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

KOOC

2016/12/19 12:30

aglkjggg様教えていただきありがとうございます。そうならないためにはどのようにコードを書けばいいでしょうか…
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問