由NumberFormatException引起的类ImageView错误

时间:2016-07-27 10:29:54

标签: android android-layout android-compatibility

我最近在Android Spinner的{​​{1}}方法中夸大了getView()项时,从Android 4.2设备收到了这个奇怪的错误:

ArrayAdapter

这是项目布局的XML:

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)

这就是崩溃的路线:

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

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

2 个答案:

答案 0 :(得分:9)

问题在于 // SpinnerAdapter.java @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(getContext(), RESOURCE, null); <-- Crash } ... } 将被分配一种颜色,而不是android:tint=""所选择的选择器。

直接使用其值@android:color/tertiary_text_light或通过资源值en #808080将有效colors.xml

答案 1 :(得分:1)

ImageView的色调是在Android API 21中引入的。如果要在API 21以下使用它,则可以使用android.support.v7.widget.AppCompatImageView;如果您在XML中声明该色调为选择器,则应替换“ android:tint ”和xml中的“ 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" />