こんにちは、私は Android の初心者で、Android を勉強するために基本的な数独アプリケーションを実行しています。ゲームを開始しようとするとクラッシュし、エミュレーターのメニュー画面に戻ります。誰か助けてください..
前もって感謝します。
以下は私のコードです:
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);
}
}
}
}
ログキャット:
06-09 10:51:30.163: E/AndroidRuntime(2266): at org.example.sudoku.puzzleview.onDraw(puzzleview.java:95)