由 NumberFormatException 引起的 class ImageView 膨胀错误

Error inflating class ImageView caused by NumberFormatException

我最近在 Android 4.2 设备 ArrayAdaptergetView() 方法中对 Spinner 项目充气时收到这个奇怪的错误:

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)

这是项目布局的XML:

<!-- 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>

这是它崩溃的地方:

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

那么,这里出了什么问题? Android Studio 没有向我发出任何警告,应用程序在我测试过的所有设备上编译和运行都很好...

问题只是要为 android:tint="" 分配颜色,而不是选择器,而 @android:color/tertiary_text_light 是。

直接使用其值 #808080 或通过资源值 en colors.xml 将起作用 @colors/gray_light

ImageView的tint是在Android API 21中介绍的,如果想在API 21以下使用可以使用android.support.v7.widget.AppCompatImageView and如果您在 xml 中将色调声明为选择器,则应在 xml

中将 "android:tint" 替换为 "app:tint"

这是一个例子:

<android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_apply"
        app:tint="@color/button_selector" />