首页 > 代码库 > android运行时异常:Binary XML file line # : Error inflating class

android运行时异常:Binary XML file line # : Error inflating class

自定义View时出现的,搞了好半天终于搞好了。

继承View时要要实现它所有的构造函数。

如:

 1 public class hongdian extends View {
 2     float x=40;
 3     float y=50;
 4     Paint p = new Paint();
 5    //要实现的构造
 6     public hongdian(Context context) {
 7         super(context);
 8     }
 9    //要实现的构造函数 
10     public hongdian(Context context, @Nullable AttributeSet attrs) {
11         super(context, attrs);
12     }
13     //要实现的构造函数
14     public hongdian(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
15         super(context, attrs, defStyleAttr);
16     }
17       //要实现的构造函数
18     public hongdian(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
19         super(context, attrs, defStyleAttr, defStyleRes);
20     }
21 
22     @Override
23     protected void onDraw(Canvas canvas) {
24         super.onDraw(canvas);
25         p.setColor(Color.BLACK);
26         canvas.drawCircle(x,y,15,p);
27     }
28 
29     @Override
30     public boolean onTouchEvent(MotionEvent event) {
31         x = event.getX();
32         y = event.getY();
33         this.invalidate();
34         return true;
35     }
36 }

 

android运行时异常:Binary XML file line # : Error inflating class