开发过程中出现错误
android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class TextView
Caused by: android.content.res.ResourcesNotFoundException: Drawable com.jingcai.apps.qualitydev:color/text_selector_white_gray with resource ID #0x7f0d00ef
Caused by: android.content.res.ResourcesNotFoundException: File res/color/text_selector_white_gray.xml from drawable resource ID #0x7f0d00ef
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:725)
…………………………
…………………………
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a ‘drawable’ attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:183)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:116)
at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1224)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1197)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:715)
追踪得到布局文件看TextView的一个属性出错
布局文件如下
<TextView
android:id="@+id/dynamic_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/text_selector_white_gray"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:minHeight="@dimen/btn_height"
android:text="" />
报错属性 android:background=”@color/text_selector_white_gray”
text_selector_white_gray是一个颜色选择器,查询得到
View的background不能用color类型(@color文件夹下面)的颜色选择器,否则就会报上面的错误。但是可以用color类型的非选择器的颜色如android:background=”@color/white”,是可以的,或者drawable类型。强调一下,不能用color类型颜色选择器!!!TextColor可以用于
color类型颜色选择器。
解决办法:color类型颜色选择器drawable改成类型的选择器
以下是的color文件夹下的原文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/text_gray_light" android:state_enabled="false"/>
<item android:color="@color/text_dark" android:state_enabled="true"/>
</selector>
将color属性改成drawable属性,同时引用的资源也改成drawable类型,如下文件,将此文件放在drawable目录下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/text_gray_light" android:state_enabled="false"/>
<item android:drawable="@drawable/text_dark" android:state_enabled="true"/>
</selector>
另外,两个颜色引用写在colors.xml,记住是drawable标签颜色写在colors.xml文件中,不是color标签
<drawable name="text_gray_light">#999999</color>
<drawable name="text_dark">#333333</color>
最后一步修改引用
<TextView
android:id="@+id/dynamic_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_selector_white_gray"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:minHeight="@dimen/btn_height"
android:text="" />