Ton cs Ton cs - 3 months ago 25
Java Question

"ArrayAdapter requires the resource ID to be a TextView" sometimes in time execution

i am using crashlityc and was looking for this error for several hours. it works, but with some users i have this error:


android.widget.ArrayAdapter.createViewFromResource
(ArrayAdapter.java:394) android.widget.ArrayAdapter.getView
(ArrayAdapter.java:362) com.android.internal.os.ZygoteInit.main
android.widget.AbsSpinner.onMeasure (AbsSpinner.java:197)
android.widget.Spinner.onMeasure (Spinner.java:541)
android.support.v7.widget.AppCompatSpinner.onMeasure (AppCompatSpinner.java:418)
android.view.View.measure (View.java:17951)
(ZygoteInit.java:1200)


In my class i have this:

private void setPhoneNumbers() {
... code .......
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, numbers);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mPhoneNumbersSpinner.setAdapter(spinnerAdapter);
}`


In my .xml :

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginRight="5dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/menu_header_txt"
android:textSize="13sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/text1" />`

Answer

I suppose you need to use the following ArrayAdapter constructor mentioned here:

ArrayAdapter (Context context, 
                int resource, 
                int textViewResourceId, 
                T[] objects)

In your case:

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, R.id.text1, numbers);
Comments