【问题标题】:Error while selecting radio buttons after creating dynamic UI in Android在 Android 中创建动态 UI 后选择单选按钮时出错
【发布时间】:2020-09-07 16:27:03
【问题描述】:

大家好,我正在创建一个测验应用程序,在该应用程序中我正在为多项选择题创建一个动态 UI 它工作得很好但是当我在问题上选择单选按钮然后去下一个问题并选择单选按钮时我给了我错误 "java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioButton " 此外,当我更改所选选项示例:首先我选择问题的选项 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.access$600(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.access$3800(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)

我该如何克服这个问题,请帮助我提前谢谢

【问题讨论】:

    标签: android android-studio radio-button


    【解决方案1】:

    tv.setId(i);b1.setId(j); - 不要那样做......你不应该像这样使用/设置 id。所有Views id 应该(必须?)在整个应用程序中是唯一的,在您的情况下,一个TextView 和少数RadioButton 可能具有相同的id(例如1)。查看THIS SO 主题以了解更多信息如何以编程方式为Views 设置正确的ID

    您正在为TextViewRadioButton 设置id 和小int,因此当您调用findViewById 时,它将返回第一个找到的View 并带有此ID - 在您的情况下为TextView,但是您将其投射到RadioButton...

    【讨论】:

    • 我能做些什么来解决这个问题,然后我应该在 id 旁边输入一些字符串
    • 更改 ID,它们是重复的。最简单的方法:b1.setId(j); -> b1.setId(i*100+j);,但这不是好方法,请阅读答案中链接的主题
    • @snachsm 谢谢先生,您的回答对我有很大帮助,现在我正在使用 View.generateViewId(),它对我很有帮助
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多