1.刚刚写好一个Demo:
package com.example.canvastest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by 700 on 2018/7/30.
*/
public class DrawRectView extends View{
//1/4 定义变量
private Paint mRectPaintStroke;
private Paint mRectPaintFill;
private int[] rect1 = new int[]{10, 10 , 110, 110};
private int[] rect2 = new int[]{10, 310 , 110, 410};
//2/4 三个构造函数
public DrawRectView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DrawRectView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawRectView(Context context) {
super(context);
}
//3/4 初始化画笔
private void initView(){
mRectPaintFill.setColor(getResources().getColor(
android.R.color.holo_blue_bright));
mRectPaintFill.setStyle(Paint.Style.FILL);
mRectPaintStroke.setColor(getResources().getColor(
android.R.color.holo_blue_bright));
mRectPaintStroke.setStyle(Paint.Style.STROKE);
}
//4/4 开画
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(rect1[0],rect1[1],rect1[2],rect1[3],mRectPaintStroke);
canvas.drawRect(rect2[0],rect2[1],rect2[2],rect2[3],mRectPaintFill);
}
}
2.一运行:
07-29 14:34:45.660 4468-4468/com.example.canvastest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.canvastest, PID: 4468
java.lang.NullPointerException: Attempt to read from field 'long android.graphics.Paint.mNativePaint' on a null object reference
at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:843)
at com.example.canvastest.DrawRectView.onDraw(DrawRectView.java:50)
at android.view.View.draw(View.java:15114)
at android.view.View.updateDisplayListIfDirty(View.java:14048)
at android.view.View.getDisplayList(View.java:14071)
at android.view.View.draw(View.java:14838)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14043)
at android.view.View.getDisplayList(View.java:14071)
at android.view.View.draw(View.java:14838)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14043)
at android.view.View.getDisplayList(View.java:14071)
at android.view.View.draw(View.java:14838)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14043)
at android.view.View.getDisplayList(View.java:14071)
at android.view.View.draw(View.java:14838)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15117)
at android.widget.FrameLayout.draw(FrameLayout.java:592)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2595)
at android.view.View.updateDisplayListIfDirty(View.java:14048)
at android.view.View.getDisplayList(View.java:14071)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:266)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:272)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:311)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2492)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2337)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-29 14:34:47.589 4468-4468/com.example.canvastest I/Process: Sending signal. PID: 4468 SIG: 9
3.呵呵哒,看了一下,发现就是写好了忘记调用 initView()了 ←_←。。。
好的加在onDraw()里面,再来。。。。
有没有搞错啊。。。
又看了一下。。。好的。。。
几个意思捏,就是缩声明了变量之后,没给它new一下啊 ←_←。。。
好的,在onDraw里面给你new了,可以了吧。。。可以了
真是饶人的小妖精,新车上路,注意系好安全带
那个,就是在学绘图机制,过阵子写个简书总结一下昂
网友评论