Android自定义View(一) 画线段

public class MusiceView extends View {
    Paint mPaint;
    public MusiceView(Context context) {
       super(context);
       mPaint = new Paint();
       mPaint.setColor(getResources().getColor(android.R.color.holo_blue_dark));
       mPaint.setStrokeWidth(20);

    }
    public MusiceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public MusiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(0,0,0,100,mPaint);
    }
}
 
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.superdy.test.MainActivity">

    <com.example.superdy.test.MusiceView
        android:layout_centerInParent="true"
        android:layout_width="100dp"
        android:layout_height="100dp"
     />

</RelativeLayout>

 
报错
     
        java.lang.NullPointerException: Attempt to invoke virtual method 'long 
android.graphics.Paint.getNativeInstance()' on a null object reference
 
为什么
 
 
我们翻开源码看看注释
/**
 * Simple constructor to use when creating a view from code.
 *
 * @param context The Context the view is running in, through which it can
 *        access the current theme, resources, etc.
 */
public View(Context context) {
。。。
 
 
/**
 * Constructor that is called when inflating a view from XML. This is called
 * when a view is being constructed from an XML file, supplying attributes
 * that were specified in the XML file. This version uses a default style of
 * 0, so the only attribute values applied are those in the Context's Theme
 * and the given AttributeSet.
 *
 * <p>
 * The method onFinishInflate() will be called after all children have been
 * added.
 *
 * @param context The Context the view is running in, through which it can
 *        access the current theme, resources, etc.
 * @param attrs The attributes of the XML tag that is inflating the view.
 * @see #View(Context, AttributeSet, int)
 */
public View(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

===========================
原因明了了
 
第一个构造方法时在代码中创建view的时候可以使用的
而第二个构造方法则是在xml中创建view的时候使用的。
 
我们修改下代码
 
public class MusiceView extends View {
    Paint mPaint;
    public MusiceView(Context context) {
        super(context);
    }
    public MusiceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(android.R.color.holo_blue_dark));
        mPaint.setStrokeWidth(20);
    }
    public MusiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(0,0,0,100,mPaint);
    }
}

这次成功了,画出了一条线段。
 

 
                                                                         
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值