自己的项目在Pad上一直运行正常,可是当运行在手机上时在一个Inflater.inflater()解析布局时出现了运行时异常“ Binary XML file line # : Error inflating class”,而我根据提示的行数找到xml文件中发现该行是TextView,最后通过查找资料发现问题是因为我的TextView中设置了属性textColor,但是它引用在color值只在values-large目录下才有,values目录下没有!将该color值copy到values目录下就解决了问题~
所以如果根据出错信息找到的是系统的View话可能的原因就是引用的资源没有放在合适的文件夹下。但这个问题应该更多的出现在自定义View时,如果是自定义的View,那么可能有下面几个原因:
1.XML中自定义view的标签使用错误。标签的格式应该是 包名 + . + 类名。
2.自定义View时三个构造函数都要实现
View(Context context) //Simple constructor to use when creating a view from code
View(Context context, AttributeSet attrs) //Constructor that is called when inflating a view from XML
View(Context context, AttributeSet attrs, int defStyle) //Perform inflation from XML and apply a class-specific base style
第一个是用来在代码中创建View使用,第二个和第三个是从xml中创建View时使用
3.编译的中间文件没有清理干净:日不你在原生系统代码的编译环境下编译APK之后,特别是修改了XML,出现标题所述现象,这个时候你只需要删除out目录下编译生成的中间文件夹即可(具体名字记不清了:在编译过程中,系统会将那个位置打印出来,通过串口来看吧,.../out/....../..../classes.dex,循着这个路径往前推到你的应用的project名字那一层文件夹),删除再重新make就OK了。
4.XML语法有隐蔽错误
例如 android:textSize="@style/Text.Mediumn"
今天在学习SurfaceView时偶然发现并解决了之前疑惑小编很久的问题:
在Android开发中,制作自己的自定义组件的时候,总是会遇到Binary XML file line Error #**:Error inflating class com.**.**。而且这个问题还不总是出现,在我做某些自定义组件的时候就会出现,某些就不会。。。
我当时的activity_main.xml文件中是这样定义组件的:
- <com.example.MySurfaceView
- android:id="@+id/game_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- public class MySurfaceView extends SurfaceView {
- public MySurfaceView(Context context) {
- super(context);
- }
- ......
- }
好了,现象描述完了。接下来小编先说怎样改再根据代码说一下原因。
把上述的MySurfaceView.java改成形如下面:
- public class MySurfaceView extends SurfaceView {
- public MySurfaceView(Context context) {
- super(context);
- }
- public MySurfaceView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- ......
- }
这个问题比较隐晦,着实困扰了小编很久,现在算是记住啦,也希望新手看客老爷们也多多注意这一点~
参考:
http://blog.csdn.net/huangxiaohu_coder/article/details/8497286http://stackoverflow.com/questions/3739661/error-inflating-when-extending-a-class
转载地址:
http://blog.csdn.net/Samuel__Liu/article/details/53761683
http://blog.csdn.net/xiaoxuantengkong/article/details/40864065