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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM