Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布局xml中具有自定义视图的空指针异常_Java_Android_Eclipse_Android Custom View_Custom View - Fatal编程技术网

Java 布局xml中具有自定义视图的空指针异常

Java 布局xml中具有自定义视图的空指针异常,java,android,eclipse,android-custom-view,custom-view,Java,Android,Eclipse,Android Custom View,Custom View,以下是自定义视图代码,它在使用此视图的XML的布局选项卡中为我提供了一个空指针异常: public class BoardView extends View { // Drawables for the board boxes, the playable zone public Drawable[][] block = new Drawable[20][10]; // Drawables for the wall (yes, it's done with tiles)

以下是自定义视图代码,它在使用此视图的XML的布局选项卡中为我提供了一个空指针异常:

public class BoardView extends View {

    // Drawables for the board boxes, the playable zone
    public Drawable[][] block = new Drawable[20][10];
    // Drawables for the wall (yes, it's done with tiles)
    public Drawable[] wall = new Drawable[102];
    // Drawable for the background, and boolean for drawing it or not
    Drawable mbg;
    boolean bg = false;
    // Context and canvas to be used along the class
    Context context;
    Canvas c;

    /*************************************************/
    /* Class constructor **************************** */
    /*************************************************/
    /* Defines the context and the canvas *********** */
    /*************************************************/
    public BoardView(Context cont, AttributeSet attrs) {
        super(cont, attrs);
        context = cont;
    }

    /*************************************************/
    /* Initializes drawables for playable boxes ***** */
    /*************************************************/
    /* Must be initialized one by one from the Game * */
    /* activity, passing all the parameters ********* */
    /*************************************************/
    public void initialize(int i, int j, int left, int top, int side) {
        block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
        block[i][j].setBounds(left, top, left + side, top + side);
    }

    /*************************************************/
    /* Draws the board wall ************************* */
    /*************************************************/
    /* Needs the top-left point of the board frame ** */
    /* and the width of the wall ******************** */
    /*************************************************/
    public void createWall(int left, int top, int side) {
        int i = 0, x, y;
        x = left - side / 2;
        y = top;
        // The left wall
        while (i < 40) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            y = y + side / 2;
            i = i + 1;
        }
        x = left + side * 10;
        y = top;
        // The right wall
        while (i < 80) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            y = y + side / 2;
            i = i + 1;
        }
        x = left - side / 2;
        // The floor
        while (i < 102) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            x = x + side / 2;
            i = i + 1;
        }
    }

    /*************************************************/
    /* Draws the board background ******************* */
    /*************************************************/
    /* Needs the top-left point of the board frame ** */
    /* and the width of the wall ******************** */
    /*************************************************/
    public void createBg(int left, int top, int side) {
        // Set board background (if any)
        bg = false;
        int bgn = 1 + (int) (Math.random() * 19);
        switch (bgn) {
        case 1:
            mbg = getResources().getDrawable(R.drawable.bg1);
            break;
        case 2:
            mbg = getResources().getDrawable(R.drawable.bg2);
            break;
        case 3:
            mbg = getResources().getDrawable(R.drawable.bg3);
            break;
        case 4:
            mbg = getResources().getDrawable(R.drawable.bg4);
            break;
        case 5:
            mbg = getResources().getDrawable(R.drawable.bg5);
            break;
        case 6:
            mbg = getResources().getDrawable(R.drawable.bg6);
            break;
        case 7:
            mbg = getResources().getDrawable(R.drawable.bg7);
            break;
        case 8:
            mbg = getResources().getDrawable(R.drawable.bg8);
            break;
        case 9:
            mbg = getResources().getDrawable(R.drawable.bg9);
            break;
        case 10:
            mbg = getResources().getDrawable(R.drawable.bg11);
            break;
        case 11:
            mbg = getResources().getDrawable(R.drawable.bg11);
            break;
        case 12:
            mbg = getResources().getDrawable(R.drawable.bg12);
            break;
        case 13:
            mbg = getResources().getDrawable(R.drawable.bg13);
            break;
        case 14:
            mbg = getResources().getDrawable(R.drawable.bg14);
            break;
        case 15:
            mbg = getResources().getDrawable(R.drawable.bg15);
            break;
        case 16:
            mbg = getResources().getDrawable(R.drawable.bg16);
            break;
        case 17:
            mbg = getResources().getDrawable(R.drawable.bg17);
            break;
        case 18:
            mbg = getResources().getDrawable(R.drawable.bg18);
            break;
        case 19:
            mbg = getResources().getDrawable(R.drawable.bg19);
            break;
        }
        mbg.setBounds((int) (left), (int) (top), (int) (left + side * 10),
                (int) (top + 20 * side));
    }

    /*************************************************/
    /* Draws the board ****************************** */
    /*************************************************/
    /* Draws the walls, the bg and all the boxes **** */
    /*************************************************/
    @Override
    protected void onDraw(Canvas canvas) {
        c = canvas;
        super.onDraw(canvas);
        if (bg)
            mbg.draw(canvas);
        for (int i = 0; i < 102; i++)
            wall[i].draw(c);
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 10; j++) {
                block[i][j].draw(canvas);
            }
        // Actually draw
        invalidate();
    }

    /*************************************************/
    /* Canvas getter ******************************** */
    /*************************************************/
    public Canvas getCanvas() {
        return c;
    }

    /*************************************************/
    /* Colors a box ********************************* */
    /*************************************************/
    /* Changes the drawable for the indicated box to */
    /* to 'c'. Can also be COLOR_NONE to undraw ***** */
    /*************************************************/
    public void setColor(int i, int j, byte c) {
        Rect rect;
        rect = block[i][j].getBounds();
        switch (c) {
        case Values.COLOR_NONE:
            block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
            ;
            break;
        case Values.COLOR_RED:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_red);
            break;
        case Values.COLOR_GREEN:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_green);
            break;
        case Values.COLOR_BLUE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_blue);
            break;
        case Values.COLOR_YELLOW:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_yellow);
            break;
        case Values.COLOR_PINK:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_pink);
            break;
        case Values.COLOR_PURPLE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_purple);
            break;
        case Values.COLOR_WHITE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_white);
            break;
        }
        block[i][j].setBounds(rect);
    }
}
下面是BoardView.java的第166行:

wall[i].draw(c);

我怎样才能解决这个问题?我从一个俄罗斯方块源代码中得到了这段代码,我想研究它以备将来参考,但我无法查看游戏的布局,我看不到初始化方法的实际调用

例如:

initialize(i, j, left, top, side);
所以我猜你是从外面这样做的,错过了一些东西,所以墙、块或mbg仍然是空的

如果提示不够,可以添加一些代码,说明如何使用和初始化视图


(或者,希望不是这样,我错了!)

您当前没有执行任何
NULL
检查您的
onDraw()

签入
NULL
将帮助您生成更稳定的代码,并帮助您更快地发现问题

我将添加一些
NULL
检查,如下所示:

protected void onDraw(Canvas canvas) {
    if (canvas != null) {
        c = canvas;
        super.onDraw(canvas);
        if (bg) {
            if (mbg != null) {
                mbg.draw(canvas);
            } else {
                /*
                 * Handle null value
                 * print to logcat this was null
                 */
            }
        }
        for (int i = 0; i < 102; i++) {
            if (wall[i] != null) {
                wall[i].draw(c);
            } else {
                /*
                 * Handle null value
                 * print to logcat this was null
                 */
            }
        }
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 10; j++) {
                if (block[i][j] != null) {
                    block[i][j].draw(canvas);
                } else {
                    /*
                     * Handle null value
                     * print to logcat this was null
                     */
                }
            }
        }
        //Actually draw
        invalidate();
    } else {
        /*
         * Handle null value
         * print to logcat this was null
         */
    }

}
受保护的void onDraw(画布){
if(canvas!=null){
c=帆布;
super.onDraw(帆布);
如果(背景){
如果(mbg!=null){
mbg.绘图(画布);
}否则{
/*
*句柄空值
*打印到logcat这是空的
*/
}
}
对于(int i=0;i<102;i++){
如果(墙[i]!=null){
墙[i]。图(c);
}否则{
/*
*句柄空值
*打印到logcat这是空的
*/
}
}
对于(int i=0;i<20;i++){
对于(int j=0;j<10;j++){
if(块[i][j]!=null){
块[i][j]。绘制(画布);
}否则{
/*
*句柄空值
*打印到logcat这是空的
*/
}
}
}
//实际绘制
使无效();
}否则{
/*
*句柄空值
*打印到logcat这是空的
*/
}
}

因此堆栈跟踪告诉您在第166行获得了一个
NullPointerException
,它位于
BoardView.onDraw
方法内。你能自己发布那行吗?来吧@Jan Smith,我们在等那行=)你还需要布局的xml吗?那么,你必须检查
wall
中的
Drawable
s是否为空你怎么+1?D:对不起,我是新来的在这个答案的顶部有一些上下箭头和一个复选标记。选中复选标记,然后单击向上箭头至+1。=)D:我给你的问题打了个+1分,这样你就可以更接近这个声誉要求了。你到了15岁以后就可以回来了。因此,这是一个非常有用的社区,可以随时使用。谢谢,祝你好运。
protected void onDraw(Canvas canvas) {
    if (canvas != null) {
        c = canvas;
        super.onDraw(canvas);
        if (bg) {
            if (mbg != null) {
                mbg.draw(canvas);
            } else {
                /*
                 * Handle null value
                 * print to logcat this was null
                 */
            }
        }
        for (int i = 0; i < 102; i++) {
            if (wall[i] != null) {
                wall[i].draw(c);
            } else {
                /*
                 * Handle null value
                 * print to logcat this was null
                 */
            }
        }
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 10; j++) {
                if (block[i][j] != null) {
                    block[i][j].draw(canvas);
                } else {
                    /*
                     * Handle null value
                     * print to logcat this was null
                     */
                }
            }
        }
        //Actually draw
        invalidate();
    } else {
        /*
         * Handle null value
         * print to logcat this was null
         */
    }

}