An exception occurs when the default Android Button is rewritten:
Caused by: android. view. InflateException: Binary XML file line #11: Error inflating class com. example. activity1.TestButton
The code for the custom control is as follows, but the onTouchEvent method is simply rewritten and cannot be used normally.
Public class TestButton extends Button {public TestButton (Context context) {super (context); // TODO Auto-generated constructor stub} @ Overridepublic boolean onTouchEvent (MotionEvent event) {boolean value = super. onTouchEvent (event); System. out. println ("super. onTouchEvent: "+ value); return value ;}}
The error is reported because a constructor is added with the (Context, AttributeSet) parameter. The second parameter is used to initialize the attributes in the xml file.
To use a custom control in an xml file, you must override the constructor with the preceding two parameters. After being added, it can be used normally.
Public TestButton (Context context, AttributeSet attributeSet) {super (context, attributeSet); // TODO Auto-generated constructor stub}