textview casting error:- android.widget.LinearLayout cannot be cast to android.widget.TextView

ぃ、小莉子 提交于 2019-12-25 02:52:24

问题


please help to solve this problem , in this i am trying to get the list of countries in the spinner along with it's flag And here is the code: I have called the spinner object with in the fragment

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));

And the countriesListAdapter is :

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();
}

}

Here is my 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)

       


回答1:


Try this in the constructor for CountriesListAdapter class:

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

instead of:

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

I just tried it and it worked.

Edit: I also had to modify your layout as the TextView was interfering with the 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>

And the reason why the country images were not showing up in the selector list was because you also have to override getDropDownView in CountriesListAdapter and do the same thing that is being done in getView like so:

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

I tried it with:

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

and drawable resource images zh_cn.png, ja_jp.png, ko_kr.png and zh_tw.png




回答2:


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

To

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



来源:https://stackoverflow.com/questions/29291526/textview-casting-error-android-widget-linearlayout-cannot-be-cast-to-android-w

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