Binary XML file line : Error inflating class <unknown> 解决
来源:互联网 发布:雪鹰领主法器进阶数据 编辑:程序博客网 时间:2024/05/21 11:00
思路:1.官方文档
2.百度、谷歌
情况解问题:今天在捣鼓了button,我想做个这样的效果:不同的state来触发不同的按钮背景颜色。我当时为了测试就简单写了个:
新建res/drawable/selector.xml:
selector.xml
<?xmlversion="1.0"encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#3197FF"/></selector>
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="这是button" android:background="@drawable/selector"/></LinearLayout>
运行项目出现错误:<item> tag requires a 'drawable' attribute or child tag defining a drawable 大概的意思就是:item标签需要一个drawable属性或者一个子标签来定义一个drawable属性。
于是我去官网查看了selector相关信息(http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList),发现关于Button有这样一段代码:
EXAMPLE:XML file saved at res/drawable/button.xml:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:state_hovered="true" android:drawable="@drawable/button_focused" /> <!-- hovered --> <item android:drawable="@drawable/button_normal" /> <!-- default --></selector>This layout XML applies the state list drawable to a Button:<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
发现 android:drawable="@drawable/button_pressed" 这行,但是还是一头雾水,好吧,我直接百度问题,找到这个问题解决方案http://www.360doc.com/content/13/0503/11/12091541_282627062.shtml,自己看了下。
解决方案一:
最后这样修改就出效果了
selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/juhuangse"/> </selector>
新建res/value/colors.xml:
colors.xml
<?xml version="1.0" encoding="utf-8"?><resources> <drawable name="juhuangse">#EE9A00</drawable> </resources>
main.xml不变
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="这是button" android:background="@drawable/selector"/></LinearLayout>
运行项目,效果OK了,这里没贴些效果图。
为什么这样修改就可以呢?大概原因正如上面一篇解决方案里面说的,关于修改背景,你不能使用color 属性的selector,应该使用drawable属性的selector。
解决方案二:
另一种稍微繁琐的解决方案
新建res/drawable/selector.xml:
selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/pressed"/> </selector>
新建res/drawable/pressed.xml:
pressed.xml
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@drawable/juhuangse" /></shape>
main.xml以及colors.xml同第一个解决方案。
- Binary XML file line : Error inflating class <unknown> 解决
- Binary XML file line : Error inflating class <unknown> 解决
- Android Binary XML file line : Error inflating class <unknown
- Binary XML file line : Error inflating class <unknown>
- InflateException:Binary XML file line #105: Error inflating class <unknown>
- Binary XML file line #2: Error inflating class <unknown>
- android.view.InflateException: Binary XML file line #2: Error inflating class <unknown> bug解决
- 解决Binary XML file line #6: : Error inflating class <unknown> 的问题
- 解决Binary XML file line #6: : Error inflating class <unknown> 的问题
- 解决--android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
- 解决-LogCat:Binary XML file line # : Error inflating class fragment
- Binary XML file line # : Error inflating class
- android.view.InflateException: Binary XML file line #79: Error inflating class <unknown>
- android.view.InflateException: Binary XML file line #29: Error inflating class <unknown> 解决方法
- android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
- 关于android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>问题
- android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
- android.view.InflateException: Binary XML file line #79: Error inflating class <unknown>
- boost序列化
- new ActiveXObject("Scripting.FileSystemObject") 时抛出异常,解决方案方法
- log4j配置
- 10044-Erdos number Time limit exceeded
- Cocos2d游戏开发常用工具集合
- Binary XML file line : Error inflating class <unknown> 解决
- 一个人的冲锋
- 认识大数据分析
- float unsinged int
- Linux下LibCurl编程
- 分布式引起的cursor: pin S wait on X 事件问题处理
- 难复现的复杂sql
- Spring事务配置的五种方式
- 华赛S3900扩容