textview cast error: - android.widget.LinearLayout无法强制转换为android.widget.TextView

时间:2015-03-27 00:53:19

标签: android android-layout android-spinner

请帮助解决这个问题,在这里我试图获得微调器中的国家列表以及它的标志这里是代码:我在片段中调用了旋转器对象

v=inflater.inflate(R.layout.view_flipper_activity,container, false);
            Spinner citizenship = (Spinner)v.findViewById(R.id.country_spinner);
            citizenship.setAdapter(new CountriesListAdapter(getActivity(),recourseList));

countriesListAdapter是:

public class CountriesListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public CountriesListAdapter(Context context,String[] values){
    super(context,R.layout.country_list_item,values);
    this.context=context;
    this.values=values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView= (TextView) rowView.findViewById(R.id.txtViewCountryName);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String[] g=values[position].split(",");
    textView.setText(GetCountryZipCode(g[1]).trim());


    String pngName = g[1].trim().toLowerCase();
    imageView.setImageResource(context.getResources().getIdentifier("drawable/" + pngName, null, context.getPackageName()));
    return rowView;
}

private String GetCountryZipCode(String ssid){
    Locale loc = new Locale("", ssid);

    return loc.getDisplayCountry().trim();
}

}

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<ImageView
    android:id="@+id/imgViewFlag"
    android:layout_height="40dp"
    android:layout_width="40dp"
    android:layout_margin="5dp" />

<TextView
    android:id="@+id/txtViewCountryName"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="hello"
    android:textSize="20dp"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/imgViewFlag" />

</RelativeLayout>

logcat的:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
        at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
        at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:896)
        at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:892)
        at android.widget.Spinner.measureContentWidth(Spinner.java:772)
        at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1144)
        at android.widget.Spinner$DropdownPopup.show(Spinner.java:1173)
        at android.widget.Spinner.performClick(Spinner.java:685)
        at android.view.View$PerformClick.run(View.java:19311)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5692)
        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:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView


    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379) 
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
   at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:896)

2 个答案:

答案 0 :(得分:3)

CountriesListAdapter class:

的构造函数中尝试此操作

super(context, R.layout.country_list_item, R.id.txtViewCountryName, values);

而不是:

super(context,R.layout.country_list_item,values);

我刚尝试过它并且有效。

编辑: 我还必须修改您的布局,因为TextView干扰了ImageView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <ImageView
        android:id="@+id/imgViewFlag"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/txtViewCountryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/imgViewFlag"
        android:layout_toRightOf="@id/imgViewFlag"
        android:text="hello"
        android:textAppearance="@android:style/TextAppearance.Medium.Inverse" />

</RelativeLayout>

国家/地区图片未显示在选择器列表中的原因是因为您还必须覆盖getDropDownView中的CountriesListAdapter并执行与getView相同的操作像这样:

@Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

我尝试过:

values = new String[]{ "CHINA,zh_CN", "JAPAN,ja_JP", "KOREA,ko_KR", "TAIWAN,zh_TW" }

drawable资源图片zh_cn.pngja_jp.pngko_kr.pngzh_tw.png

答案 1 :(得分:0)

更改代码String[] g=values[position].split(","); textView.setText(GetCountryZipCode(g[1]).trim());

String[] g=values[position].split(","); String k= g[1]; textView.setText((GetCountryZipCode[k]).trim());