Search code examples
androidandroid-layoutandroid-compatibility

Error inflating class ImageView caused by NumberFormatException


I recently got this weird error from a Android 4.2 device when inflating a Spinner item in the getView() method of its ArrayAdapter:

07-27 10:44:18.120 23988-23988/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: xxx.xxx.xxx, PID: 23988
                                                   android.view.InflateException: Binary XML file line #21: Error inflating class ImageView
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                       at android.view.View.inflate(View.java:17832)
                                                       at xxx.xxx.xxx.adapters.SpinnerAdapter.getView(SpinnerAdapter.java:37)
                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2338)
                                                       at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1605)
                                                       at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
                                                       at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1176)
                                                       at android.widget.ListPopupWindow.show(ListPopupWindow.java:554)
                                                       at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1106)
                                                       at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:980)
                                                       at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:962)
                                                       at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
                                                       at android.os.Handler.dispatchMessage(Handler.java:110)
                                                       at android.os.Looper.loop(Looper.java:193)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                       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:829)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: java.lang.NumberFormatException: Invalid int: "res/color/tertiary_text_light.xml"
                                                       at java.lang.Integer.invalidInt(Integer.java:137)
                                                       at java.lang.Integer.parse(Integer.java:374)
                                                       at java.lang.Integer.parseInt(Integer.java:365)
                                                       at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
                                                       at android.content.res.TypedArray.getInt(TypedArray.java:255)
                                                       at android.widget.ImageView.<init>(ImageView.java:155)
                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                       at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                       at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008)
                                                       at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067)
                                                       at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                       at android.view.View.inflate(View.java:17832) 
                                                       at xxx.xxx.xxx.adapters.SpinnerAdapter.getView(SpinnerAdapter.java:37) 
                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2338) 
                                                       at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1605) 
                                                       at android.widget.ListView.measureHeightOfChildren(ListView.java:1273) 
                                                       at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1176) 
                                                       at android.widget.ListPopupWindow.show(ListPopupWindow.java:554) 
                                                       at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1106) 
                                                       at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:980) 
                                                       at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:962) 
                                                       at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                       at android.os.Looper.loop(Looper.java:193) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                       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:829) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                       at dalvik.system.NativeStart.main(Native Method)

This is the XML of the item's layout:

<!-- item_autocomplete.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp"
    android:gravity="center_vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/item_autocomplete_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/primaryColor"/>

    <ImageView
        android:id="@+id/item_autocomplete_img"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:layout_below="@id/item_autocomplete_title"
        android:padding="2dp"
        android:src="@drawable/ic_location_on_black_18dp"
        android:tint="@android:color/tertiary_text_light"/>

    <TextView
        android:id="@+id/item_autocomplete_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/item_autocomplete_img"
        android:layout_toEndOf="@+id/item_autocomplete_img"
        android:layout_toRightOf="@+id/item_autocomplete_img"
        android:textColor="@android:color/tertiary_text_light"
        android:textSize="12sp"/>

</RelativeLayout>

And this is the line where it crashed:

    // SpinnerAdapter.java
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(getContext(), RESOURCE, null); <-- Crash
        }
        ...
    }

So, what's wrong here? Android Studio does not throw me any warning and the app compiles and runs just fine on all devices I have tested...


Solution

  • The problem is just that android:tint="" is to be assigned a color, not a selector, which @android:color/tertiary_text_light is.

    Using directly its value #808080 or through a resource value en colors.xml will work @colors/gray_light.