android.widget.RadioButton cannot be cast to android.widget.Spinner

痞子三分冷 提交于 2019-12-24 13:54:58

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/22583777/android-widget-radiobutton-cannot-be-cast-to-android-widget-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!