2

i m trying to get the Value from RadioButton that is Created by String of Array i m using OClickListener() to get the Value from RadioButton but Error is showing
" java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton "

Here is my Code,Please check

for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
}
       
Button btnDisplay = (Button) findViewById(R.id.button1);
btnDisplay.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View v)
    {
 // TODO Auto-generated method stub
 // get selected radio button from radioGroup
 int selectedId = radioGroup.getCheckedRadioButtonId();
 // find the radiobutton by returned id
    String s = ((RadioButton) v ).getText().toString();
 Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();
  }

and LogCat is

   
 FATAL EXCEPTION: main
 Process: com.example.radiobuttondynamic, PID: 4741
 java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
  at com.example.radiobuttondynamic.Radio_Button$1.onClick(Radio_Button.java:64)
  at android.view.View.performClick(View.java:4438)
  at android.view.View$PerformClick.run(View.java:18422)
  at android.os.Handler.handleCallback(Handler.java:733)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5017)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at dalvik.system.NativeStart.main(Native Method)

Please Give Suggestion to Correct it

thanks thanks

Amsheer
  • 7,046
  • 8
  • 47
  • 81
Ashu Kumar
  • 832
  • 19
  • 38
  • You can refer to this link : http://stackoverflow.com/questions/18179124/android-getting-value-from-selected-radiobutton – Rohhit Jan 08 '15 at 11:35
  • The problem is that you are casting the v into radio button but it actually a button. use this as a sample code. View radioButton = radioButtonGroup.findViewById(radioButtonID); if you have any query please comment. – droidd Jan 08 '15 at 12:16
  • @Vivek cau please tell me where i have to write this line of code? – Ashu Kumar Jan 08 '15 at 12:45
  • @Ashu,I posted my answer. Please refer that. – droidd Jan 08 '15 at 12:53

4 Answers4

2

v is a Button and you cast it to RadioButton in this line:

String s = ((RadioButton) v ).getText().toString();

try this:

if you added radioButton programmatically you can setID to radioButtons:

for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioButtonView.setID(i);
radioGroup.addView(radioButtonView, p);
}

and then

 String s = ((RadioButton) findViewById(yourRadioButtonID) ).getText().toString();
Hamidreza Samadi
  • 637
  • 1
  • 7
  • 24
1

Why are you trying to cast the button to radio button?? anyway if you want to get the text of selected radio button do the following

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{

  RadioButton selectedButton=   (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());  //you will get the selected radiobutton here

 String s=selectedButton..getText().toString();
 Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();

}
Praseeda t
  • 136
  • 4
0

Use this:

String s = radioButtonView.getText().toString();

Instead of:

String s = ((RadioButton) v ).getText().toString();
A.L
  • 10,259
  • 10
  • 67
  • 98
Karthikeyan
  • 1,119
  • 1
  • 15
  • 33
-1

I am writing the onclick functionality.

public void onClick(View v)
 {

  int selectedId = radioGroup.getCheckedRadioButtonId();
    if(selectedId !=-1)
     {    
        View radioButton = radioGroup.findViewById(selectedId );
        int radioId = radioGroup.indexOfChild(radioButton);
        RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
        String selection = (String) btn.getText();
    Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();
    }   
 }

Try this, If you have any problem then please comment.

droidd
  • 1,361
  • 10
  • 15