游戏开始时Sudoku崩溃了

时间:2013-06-09 12:01:01

标签: android

嗨,我是android的初学者,我正在做一个基本的数独应用程序来研究android,当我开始游戏时发现自己卡住..APP CRASH .. !!!! 每当我尝试启动游戏时,它会崩溃并将我带回模拟器中的菜单屏幕。有人请帮助我...

提前致谢。

以下是我的代码:

puzzleview.java

package org.example.sudoku;

public class puzzleview extends View {
private static  final String TAG="SUDOKU";

private float width;
private float height;
private int selX;
private int selY;
private final Rect selRect= new Rect();

private final Game game;

public puzzleview(Context context){
    super(context);
    this.game=(Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
}
@Override
protected void onSizeChanged(int w,int h,int oldh,int oldw){
width = w/9f;
height = h/9f;
getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChanged width " + width +", height "+ height);
super.onSizeChanged(w, h, oldw, oldh);
}

private void getRect(int selX2, int selY2, Rect selRect2) {
// TODO Auto-generated method stub

}
@Override
protected void onDraw(Canvas canvas){
Paint background=new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0,0, getWidth(), getHeight(), background);


Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));

Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));

Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));


for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height,
     light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height
     + 1, hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(),
     light);
canvas.drawLine(i * width + 1, 0, i * width + 1,
     getHeight(), hilite);
}
for(int i=0; i<9; i++){
if(i%3!=0)
  continue;
canvas.drawLine(0,i*height,getWidth(),i*height,dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height
      + 1, hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1,
      getHeight(), hilite);
}

Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize (height * 0.75f);
foreground.setTextScaleX(width/height);
foreground.setTextAlign(Paint.Align.CENTER);

FontMetrics fm=foreground.getFontMetrics();
float x=width/2;
float y=height/2 - (fm.ascent+fm.descent)/2;

for(int i=0;i<9;i++){

 for(int j = 0;j<9;j++){

Line 95 ---->:   canvas.drawText(this.game.getTileString(i, j), i
             * width + x, j * height + y, foreground);

      }
 }
}
}

logcat的:

06-09 10:51:30.163: E/AndroidRuntime(2266):     at      org.example.sudoku.puzzleview.onDraw(puzzleview.java:95)

2 个答案:

答案 0 :(得分:1)

这取决于您使用视图的方式,但如果您在XML中使用它,那么您需要添加另一个构造函数

public puzzleview(Context context, AttributeSet attrs){
    super(context, attrs);
    this.game=(Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
}

答案 1 :(得分:0)

我会想象第95行访问尚未初始化的内容。当应用程序。开始它很可能会尝试渲染puzzleview。猜测,game此时尚未完全初始化,因此对game.getTileString(i, j)的访问以某种方式失败。您可能希望Game中有一个标记,告诉puzzleview何时可以使用getTileString,您在puzzleview.onDraw()中使用

if (game.getTileStringsAvailable()) {
    for (int i=0;i<9;i++){
        for (int j = 0;j<9;j++){
            canvas.drawText(this.game.getTileString(i, j), i * width + x, j * height + y, foreground);
        }
    }
}
相关问题