Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class

重写Android默认Button按钮引发异常:

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.activity1.TestButton

自定义控件的代码如下,只是简单重写onTouchEvent方法,一直没办法正常使用。

public class TestButton extends Button {

	public TestButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		boolean value = super.onTouchEvent(event);
		System.out.println("super.onTouchEvent: " + value);
		return value;
	}
}

报出异常的原因是由于少添加了个一个构造方法,参数为(Context, AttributeSet),其中第二个参数用来将xml文件中的属性初始化。

自定义控件若需要在xml文件中使用,就必须重写带如上两个参数的构造方法。添加后即可正常使用了。

	public TestButton(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		// TODO Auto-generated constructor stub
	}



Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class,布布扣,bubuko.com

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class

上一篇:Android中的AlarmManager类


下一篇:Linux 下的几个命令(iostat、iotop、strace、inotifywait)