Android NullpointerException。语法错误

时间:2014-08-26 08:18:02

标签: android eclipse nullpointerexception

我已经堆叠了1天,只是为了调试一行代码。顺便说一句,我的游戏是一个使用拖动的迷宫游戏。我的游戏工作正常,直到我从其他项目获得一行代码来实现timer

以下是游戏视图的完整代码:

public class GameView extends View {

private TimeCounter timeCounter;

private boolean paused;

public boolean isPaused() {
    return paused;
}

public void setPaused(boolean paused) {
    this.paused = paused;
}    
public void setTimeCounter(TimeCounter timeCounter) {
    this.timeCounter = timeCounter;
}
// width and height of the whole maze and width of lines which
// make the walls
private int width, height, lineWidth;
// size of the maze i.e. number of cells in it
private int mazeSizeX, mazeSizeY;
// width and height of cells in the maze
float cellWidth, cellHeight;
// the following store result of cellWidth+lineWidth
// and cellHeight+lineWidth respectively
float totalCellWidth, totalCellHeight;
// the finishing point of the maze
private int mazeFinishX, mazeFinishY;
private Maze maze;
private Activity context;
private Paint line, red, background;
private String text;

public GameView(Context context, Maze maze) {
    super(context);

    this.context = (Activity) context;
    this.maze = maze;
    mazeFinishX = maze.getFinalX();
    mazeFinishY = maze.getFinalY();
    mazeSizeX = maze.getMazeWidth();
    mazeSizeY = maze.getMazeHeight();
    line = new Paint();
    line.setColor(getResources().getColor(R.color.line));
    red = new Paint();
    red.setColor(getResources().getColor(R.color.position));
    background = new Paint();
    background.setColor(getResources().getColor(R.color.game_bg));
    setFocusable(true);
    this.setFocusableInTouchMode(true);
}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = (w < h) ? w : h;
    height = width; // for now square mazes
    lineWidth = 1; // for now 1 pixel wide walls
    cellWidth = (width - ((float) mazeSizeX * lineWidth)) / mazeSizeX;
    totalCellWidth = cellWidth + lineWidth;
    cellHeight = (height - ((float) mazeSizeY * lineWidth)) / mazeSizeY;
    totalCellHeight = cellHeight + lineWidth;
    red.setTextSize(cellHeight * 0.75f);
    super.onSizeChanged(w, h, oldw, oldh);
}

protected void onDraw(Canvas canvas) {
    canvas.drawRect(0, 0, width, height, background);

    boolean[][] hLines = maze.getHorizontalLines();
    boolean[][] vLines = maze.getVerticalLines();
    // iterate over the boolean arrays to draw walls
    for (int i = 0; i < mazeSizeX; i++) {
        for (int j = 0; j < mazeSizeY; j++) {
            float x = j * totalCellWidth;
            float y = i * totalCellHeight;
            if (j < mazeSizeX - 1 && vLines[i][j]) {
                // we'll draw a vertical line
                canvas.drawLine(x + cellWidth,y,x + cellWidth,y + cellHeight,
                        line);
            }
            if (i < mazeSizeY - 1 && hLines[i][j]) {
                // we'll draw a horizontal line
                canvas.drawLine(x,y + cellHeight,x + cellWidth,y + cellHeight,
                        line);
            }
        }
    }
    int currentX = maze.getCurrentX(), currentY = maze.getCurrentY();
    canvas.drawCircle((currentX * totalCellWidth) + (cellWidth / 2),
            (currentY * totalCellHeight) + (cellWidth / 2),
            (cellWidth * 0.45f), // radius
            red);

    int secondss = timeCounter.getTimeSeconds(); // THIS IS WHERE THE ERROR.

    text = String.format("%02d:%02d", secondss/60, secondss%60);
    canvas.drawText(text, (mazeFinishX * totalCellWidth)
            + (cellWidth * 0.25f), (mazeFinishY * totalCellHeight)
            + (cellHeight * 0.75f), red); 
    }
}

以及我从其他项目中复制的 TimeCounter类以使用getTimeSeconds() method

public class TimeCounter {
    public int getTimeSeconds() {
        return (int) (getTime() / 1000);
    }
}

logcat:

08-26 16:10:02.516: E/AndroidRuntime(27100): FATAL EXCEPTION: main
08-26 16:10:02.516: E/AndroidRuntime(27100): java.lang.NullPointerException
08-26 16:10:02.516: E/AndroidRuntime(27100):    at com.jforeach.mazegame.GameView.onDraw(GameView.java:129)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.View.draw(View.java:13877)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.View.draw(View.java:13761)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.drawChild(ViewGroup.java:3039)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2903)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.View.draw(View.java:13759)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.drawChild(ViewGroup.java:3039)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2903)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.View.draw(View.java:13759)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.drawChild(ViewGroup.java:3039)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2903)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.View.draw(View.java:13880)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.widget.FrameLayout.draw(FrameLayout.java:467)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2226)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2719)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2564)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2371)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2172)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1166)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5013)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.Choreographer.doFrame(Choreographer.java:548)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.os.Handler.handleCallback(Handler.java:725)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.os.Looper.loop(Looper.java:153)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at android.app.ActivityThread.main(ActivityThread.java:5299)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-26 16:10:02.516: E/AndroidRuntime(27100):    at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。请好。我知道这不是一个有用的问题,但对于那里的某些程序员,请帮我调试这个force close error。谢谢你们,还有那些不值钱的人!

更新:在我的菜单上单击开始按钮时,它将进入游戏类,然后进入GameView类。 继承我的Game Class

public class Game extends Activity {
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    Maze maze = (Maze)extras.get("maze");
    GameView view = new GameView(this,maze);    
    setContentView(view);

这是我将调用 setTimeCounter 的地方吗?请为我这样的菜鸟道歉。

2 个答案:

答案 0 :(得分:1)

致电时

int secondss = timeCounter.getTimeSeconds();

您收到NullPointerException,因为当getTimeSeconds()timeCounter时,您尝试调用timeCounter null方法。如果对象是null,则任何访问其方法或字段的尝试都将导致此类异常。

这只是因为您忘记设置timeCounter字段。

(请注意,如果设置了NullPointerException但是timeCounter方法导致异常,您也可以从这样的行中获得getTimeSeconds()。但如果是这样,那么将在你的logcat中添加一行,告诉你导致问题的TimeCounter代码行。)

答案 1 :(得分:1)

当您对此视图进行充气时,它将在屏幕上显示视图,该视图将调用onDraw方法并在此行代码中显示:

timeCounter.getTimeSeconds();

它将被调用并将抛出异常,因为您甚至没有实例化该对象。

<强>溶液

您需要添加if块以检查timeCounter是否为空

<强>样品:

canvas.drawCircle((currentX * totalCellWidth) + (cellWidth / 2),
        (currentY * totalCellHeight) + (cellWidth / 2),
        (cellWidth * 0.45f), // radius
        red);

if(timeCounter != null) {
int secondss = timeCounter.getTimeSeconds(); // THIS IS WHERE THE ERROR.

text = String.format("%02d:%02d", secondss/60, secondss%60);

请记住,如果您使用此视图,它将直接调用onDraw,如果它是null(通过方法调用实例化的对象),则需要检查该对象