在 Android 中创建动态 UI 后选择单选按钮时出错
Error while selecting radio buttons after creating dynamic UI in Android
大家好,我正在创建一个测验应用程序,我正在为多项选择题创建动态 UI
它运作良好但是当我 select 单选按钮问题然后去下一个问题和 select 单选按钮我给我错误 "java.lang.ClassCastException: android.widget.TextView 无法转换为 android.widget.RadioButton"
另外,当我更改 selected 选项示例时:首先我选择问题的选项 1,然后将其更改为选项 2,它给了我同样的错误
这是我的代码示例
for(int i=1;i<=5;i++){
TextView tv=new TextView(this);
tv.setId(i);
RadioGroup radioGroup=new RadioGroup(this);
for(int j=0;j<=3;j++){
RadioButton b1=new RadioButton(this);
b1.setText("option"+j);
b1.setId(j);
radioGroup.addView(b1);
}
tv.setText("Youre question No "+i);
tv.setTypeface(null, Typeface.BOLD);
tv.setTextColor(Color.BLACK);
radioGroup.setOrientation(RadioGroup.VERTICAL);
tv.setPadding(20,0,0,0);
radioGroup.setPadding(0,5,0,0);
linearLayout.addView(tv);
linearLayout.addView(radioGroup);
radioGroup.setOnCheckedChangeListener(this);
}
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (radioGroup.getCheckedRadioButtonId() != -1) {
RadioButton uans = findViewById(radioGroup.getCheckedRadioButtonId());//Error occurred here
String ansText = uans.getText().toString();
answersStringArray.add(ansText);
}
}
这是例外情况:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quizapp, PID: 3428
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioButton
at com.example.quizapp.Paper.onCheckedChanged(Paper.java:99)//This shows where is the error
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:196)
at android.widget.RadioGroup.access0(RadioGroup.java:59)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:381)
at android.widget.CompoundButton.setChecked(CompoundButton.java:183)
at android.widget.CompoundButton.toggle(CompoundButton.java:135)
at android.widget.RadioButton.toggle(RadioButton.java:76)
at android.widget.CompoundButton.performClick(CompoundButton.java:140)
at android.view.View.performClickInternal(View.java:7218)
at android.view.View.access00(View.java:824)
at android.view.View$PerformClick.run(View.java:27719)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
我怎样才能克服这个问题,请大家帮助我提前谢谢
tv.setId(i);
和 b1.setId(j);
- 不要那样做......你不应该 use/set 这样的 ID。所有 View
的 ID 应该(必须?)在整个应用程序中是唯一的,在您的情况下,一个 TextView
和少数 RadioButton
可能具有相同的 ID(例如 1
)。查看 THIS SO 主题以获取更多信息如何以编程方式为 View
设置正确的 ID
你正在为 TextView
和 RadioButton
设置 id
和小 int
,所以当你调用 findViewById
时它会 return首先找到具有此 ID 的 View
- 在您的情况下为 TextView
,但您将其转换为 RadioButton
...
大家好,我正在创建一个测验应用程序,我正在为多项选择题创建动态 UI 它运作良好但是当我 select 单选按钮问题然后去下一个问题和 select 单选按钮我给我错误 "java.lang.ClassCastException: android.widget.TextView 无法转换为 android.widget.RadioButton" 另外,当我更改 selected 选项示例时:首先我选择问题的选项 1,然后将其更改为选项 2,它给了我同样的错误 这是我的代码示例
for(int i=1;i<=5;i++){
TextView tv=new TextView(this);
tv.setId(i);
RadioGroup radioGroup=new RadioGroup(this);
for(int j=0;j<=3;j++){
RadioButton b1=new RadioButton(this);
b1.setText("option"+j);
b1.setId(j);
radioGroup.addView(b1);
}
tv.setText("Youre question No "+i);
tv.setTypeface(null, Typeface.BOLD);
tv.setTextColor(Color.BLACK);
radioGroup.setOrientation(RadioGroup.VERTICAL);
tv.setPadding(20,0,0,0);
radioGroup.setPadding(0,5,0,0);
linearLayout.addView(tv);
linearLayout.addView(radioGroup);
radioGroup.setOnCheckedChangeListener(this);
}
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (radioGroup.getCheckedRadioButtonId() != -1) {
RadioButton uans = findViewById(radioGroup.getCheckedRadioButtonId());//Error occurred here
String ansText = uans.getText().toString();
answersStringArray.add(ansText);
}
}
这是例外情况:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quizapp, PID: 3428
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioButton
at com.example.quizapp.Paper.onCheckedChanged(Paper.java:99)//This shows where is the error
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:196)
at android.widget.RadioGroup.access0(RadioGroup.java:59)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:381)
at android.widget.CompoundButton.setChecked(CompoundButton.java:183)
at android.widget.CompoundButton.toggle(CompoundButton.java:135)
at android.widget.RadioButton.toggle(RadioButton.java:76)
at android.widget.CompoundButton.performClick(CompoundButton.java:140)
at android.view.View.performClickInternal(View.java:7218)
at android.view.View.access00(View.java:824)
at android.view.View$PerformClick.run(View.java:27719)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
我怎样才能克服这个问题,请大家帮助我提前谢谢
tv.setId(i);
和 b1.setId(j);
- 不要那样做......你不应该 use/set 这样的 ID。所有 View
的 ID 应该(必须?)在整个应用程序中是唯一的,在您的情况下,一个 TextView
和少数 RadioButton
可能具有相同的 ID(例如 1
)。查看 THIS SO 主题以获取更多信息如何以编程方式为 View
设置正确的 ID
你正在为 TextView
和 RadioButton
设置 id
和小 int
,所以当你调用 findViewById
时它会 return首先找到具有此 ID 的 View
- 在您的情况下为 TextView
,但您将其转换为 RadioButton
...