自定义view时遇到的android.view.InflateException: Binary XML file问题
2014-01-12 11:29
916 查看
自定义一个view控件,在写完编译成功在手机上运行时老是提示android.view.InflateException: Binary XML file的异常
查看了一遍构造函数书写也是正确,纠结了,于是重新查看了sdk的view构造方法,终于发现了问题所在。从下图可知
Added in API level 1
Simple constructor to use when creating a view from code.
Parameters
我继承view重写的控件只是重载了这一个构造方法,而从API说明,我们可以看到这个构造方法的view只是适用于从代码创建的view,而需要从xml文件inflate的view则需要重载
这个构造方法,查看sdk我们可以清楚的理解,于是重载后面的构造方法问题解决。
但是为什么呢,主要是因为(AttributeSet attrs)参数其实就是我们需要从xml文件的标签inflate后的xml文件的各种属性,从源码我们可知,构造方法开始时,需要
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
defStyleAttr, 0);
TypedArray 即为我们布局的view的属性值数组,有兴趣的可以去查看这段源码,可以大概清楚的了解,view从inflate到加载xml文件数据的基本流程
查看了一遍构造函数书写也是正确,纠结了,于是重新查看了sdk的view构造方法,终于发现了问题所在。从下图可知
public View (Context context)
Added in API level 1Simple constructor to use when creating a view from code.
Parameters
context | The Context the view is running in, through which it can access the current theme, resources, etc. |
---|
public View (Context context, AttributeSet attrs)
这个构造方法,查看sdk我们可以清楚的理解,于是重载后面的构造方法问题解决。但是为什么呢,主要是因为(AttributeSet attrs)参数其实就是我们需要从xml文件的标签inflate后的xml文件的各种属性,从源码我们可知,构造方法开始时,需要
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
defStyleAttr, 0);
TypedArray 即为我们布局的view的属性值数组,有兴趣的可以去查看这段源码,可以大概清楚的了解,view从inflate到加载xml文件数据的基本流程
相关文章推荐
- android.view.InflateException: Binary XML file line自定义view的构造函数引起
- 自定义的LinearLayout 使用报错 android.view.InflateException: Binary XML file line #3: Error inflating class
- 引用自定义View的布局异常 android.view.InflateException: Binary XML file line #5: Error inflating class ...
- 自定义View的时候,报错android.view.InflateException: Binary XML file line #0: Binary XML file line #0:
- android自定义View “android.view.InflateException: Binary XML file line # : Error inflating class"
- 自定义listview出现android.view.InflateException:Binary xml file line#
- 举例说明android编程中遇到的关于android.view.InflateException错误的原因及解决办法
- android 下自定义view, android.view.InflateException
- 自定义View时出现:"android.view.InflateException: Binary XML file line报错问题"
- android编程中遇到的Caused by: android.view.InflateException: Binary XML file line #13错误的原因及解决办法
- 移动开发:android编程中遇到的Caused by: android.view.InflateException: Binary XML file line #11: Erro错误的原因及解决办法
- (偶发闪退现象) 反复启动结束activity异常 android.view.InflateException: Binary XML file line ... Error inflating class <unknown>
- Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>错误
- android.view.InflateException: Binary XML file line #9: Error inflating class
- android.view.InflateException: Binary XML file line #异常的解决
- android.view.InflateException
- android.view.InflateException: Binary XML file line #79: Error inflating class <unknown>
- Android应用插件化开发中自定义View基础插件遇到的问题
- Android.View.InflateException: Binary XML File Line #异常的解决
- Android Studio android.view.InflateException: Error inflating class com.baidu.mapapi.map.MapView