Android自定义View(一) 画线段
来源:互联网 发布:阿里云输入法 编辑:程序博客网 时间:2024/05/18 14:27
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 0
- Android自定义View(一) 画线段
- android 连线题实现 自定义view 画线
- 自定义View(一)——画线、矩形、圆形、图像
- Android自定义View(一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android自定义View(一)
- Android 自定义View (一)
- Android 自定义View(一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android自定义View(一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android 自定义View (一)
- Android 自定义View (一)
- 字典序问题
- mysql数据库sequence使用
- 奥威Power-BI之人力资源管理驾驶舱
- 7、Ping和Traceroute程序
- 第15条:使可变性最小化
- Android自定义View(一) 画线段
- javaScript学习-动态为ul添加li
- wince整体解决方案编译错误的解决办法(StdAfx.obj' not allowed with multiple source files )
- Android数据加密之Aes加密
- Android7.0 PowerManagerService(4) Power按键流程
- 第三节:SpringBoot使用properties配置文件实现多环境配置
- 解决报错:错误1130- Host xxx is not allowed to connect to this MariaDb server
- Java基础之四大内部类(结合代码理解)
- MVC框架