java.lang.NullPointerException、field ‘’ is never assigned

简介: 1.刚刚写好一个Demo:package com.example.canvastest;import android.content.Context;import android.

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.一运行:

img_63c72292033883ff4337148baacd7ffe.png

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()里面,再来。。。。

img_8e6e0abe45d3ffc2982268adc5382a81.png

有没有搞错啊。。。
又看了一下。。。好的。。。
img_562f45445557692def5b9fa948828d94.png

几个意思捏,就是缩声明了变量之后,没给它new一下啊 ←_←。。。
好的,在onDraw里面给你new了,可以了吧。。。可以了
img_8e3420237e27090c65a830fa557bbef8.png

真是饶人的小妖精,新车上路,注意系好安全带

img_f0b2810f3a2a0c1b5edbd7640caa89a6.png

那个,就是在学绘图机制,过阵子写个简书总结一下昂

目录
相关文章
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
|
6月前
|
SQL Java 数据库连接
Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have a de
Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have a de
|
8月前
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
212 0
Can not set final java.lang.Class field org.apache.ibatis.binding.MapperProxy.mapperInterface to com
Can not set final java.lang.Class field org.apache.ibatis.binding.MapperProxy.mapperInterface to com
164 0
|
数据库
java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
|
Java
Java反射(Field)(四)
日常开发中,我们可使用反射获取JavaBean的属性信息,而描述属性信息的就是Java Field,Field类位于java.lang.reflect包下。
115 0
Java反射(Field)(四)
|
Java C++ 数据格式
Java中类的成员之一:属性(field)
Java中类的成员之一:属性(field)
311 0
Java中类的成员之一:属性(field)
|
Java
Java笔记之反射机制(Class&Package&Field)
新的知识点 底层的技术----反射技术 String类时候 隐约提起过 非常特殊的引用数据类型 String s = "abc"; //new String("abc"); 不可变特性 String不可变特性体现在 长度 内容 public final class String{ private final char[] value; } 内容好像不是真的不让变 可以通过反射技术修改 *一会儿学完了反射操作属性 尝试修改一下String中的value值 反射 reflect 面向对象的变成思想 类 从很多对象中抽取出来的公有的特征行为 抽象描述
115 0
|
Java API
Java反射—Field类使用
Java反射—Field类使用
182 0
|
NoSQL Java Redis
Java Spring Boot2.0 连接 Redis 出错:Unsatisfied dependency expressed through field 'redisRepository'
Java Spring Boot2.0 连接 Redis 出错:Unsatisfied dependency expressed through field 'redisRepository',解决办法
11755 0