1

I am using relative layout for one of my activity, but it stops, whenever i launch this activity. The error is "android.widget.RadioButton cannot be cast to android.widget.Spinner".

The xml file is as follows:

<EditText
    android:id="@+id/edit_age"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/edit_first_name"
    android:layout_below="@+id/edit_first_name"
    android:ems="10"
    android:hint="@string/age"
    android:inputType="number" >
    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner_blood_groups"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/edit_last_name"
    android:hint="@string/blood_group" />

<RadioGroup
    android:id="@+id/radio_sex"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/edit_age"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_button_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/male" />

    <RadioButton
        android:id="@+id/radio_button_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/female" />
</RadioGroup>

I am setting the width of the layout after the activity starts as follows:

public void setLayout()
{
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = metrics.widthPixels;

    RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)findViewById(R.id.edit_age).getLayoutParams();
    param.width = (int)(0.45*width);
    findViewById(R.id.edit_age).setLayoutParams(param);

    Spinner spinner = (Spinner)findViewById(R.id.spinner_blood_groups);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.blood_groups, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    param  (RelativeLayout.LayoutParams)findViewById(R.id.spinner_blood_groups).getLayoutParams();
    param.width = (int)(0.45*width);
    findViewById(R.id.spinner_blood_groups).setLayoutParams(param);
}

I am not sure where am i going wrong. Please help!

achiever202
  • 103
  • 1
  • 2
  • 8

1 Answers1

2

Sometimes i get this error when i make changes in layout files. You can try uninstalling the app from your testing device, refreshing the project and cleaning the project in Eclipse

cgr
  • 519
  • 1
  • 7
  • 20