こんにちは、Android ゲームに「画像スコア カウンター」を実装したいのですが、うまくいきません。ただし、コンパイルはうまくいきますが、ボールが画面の端に到達するとすぐにアプリがエラーを返します。
04-08 10:07:31.639: E/AndroidRuntime(8599): FATAL EXCEPTION: Thread-1176
04-08
10:07:31.639: E/AndroidRuntime(8599): java.lang.NullPointerException
04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameState.update(GameState.java:154)
04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameThread.run(GameThread.java:41)
ゲームを管理する 3 つのファイルを取得しました: GameState.java GameView.java GameThread.java:
エラーは次のとおりです。
GameState.java:
TopCounter.setImageResource(myImageList[i+1]);
GameThread.java:
_state.update();
GameThread.java は次のようになります。
package com.Pong;
import....
class GameThread extends Thread {
private SurfaceHolder surfaceHolder;
private GameView gameView;
private Paint _paint;
private GameState _state;
private boolean run = false;
public GameThread(SurfaceHolder surfaceHolder, GameView gameView, Context context, Handler handler) {
this.surfaceHolder = surfaceHolder;
this.gameView = gameView;
_paint = new Paint();
_state = new GameState();
}
public void setRunning(boolean run) {
this.run = run;
}
public SurfaceHolder getSurfaceHolder() {
return surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (run) {
c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
gameView.onDraw(c);
_state.update(); //here is the error
_state.draw(c, _paint);
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
public GameState getGameState()
{
return _state;
}
}
これは GameState.java がどのように見えるかです (私は不要な行をカットしました):
package com.Pong;
Import ...
public class GameState extends Activity implements View.OnTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamestate);
TopCounter = (ImageView) findViewById(R.id.counterT);
BottomCounter = (ImageView) findViewById(R.id.counterB);
}
int _screenWidth = 270;
int _screenHeight = 395;
int _ballSize = 10;
int _ballX = 100;
int _ballY = 100;
int _ballVelocityX = 3;
int _ballVelocityY = 3;
int _batLength = 75;
int _batHeight = 10;
int _topBatX = (_screenWidth / 2+25) - (_batLength / 2);
int _topBatY = 54;
int _bottomBatX = (_screenWidth / 2+25) - (_batLength / 2);
int _bottomBatY = 392;
int i=0;
// under is matrix that contains images to my counter.
int[] myImageList = new int[]{
R.drawable.counter00,
R.drawable.counter01,
R.drawable.counter02,
R.drawable.counter03,
R.drawable.counter04,
R.drawable.counter05,
R.drawable.counter06,
R.drawable.counter07,
R.drawable.counter08,
R.drawable.counter09
};
ImageView TopCounter;
ImageView BottomCounter;
public GameState()
{}
public void update() {
_ballX += _ballVelocityX;
_ballY += _ballVelocityY;
if( _ballY > _screenHeight){
_ballX = _screenWidth/2;
_ballY = _screenHeight/2;
TopCounter.setImageResource(myImageList[i+1]); //here is the error
_toppoint=_toppoint+1;
_topBatX =_screenWidth/2-10;
_bottomBatX =_screenWidth/2-10;
}
if( _ballY < 50){
_ballX = _screenWidth/2;
_ballY = _screenHeight/2;
BottomCounter.setImageResource(myImageList[i+1]);
_bottompoint=_bottompoint+1;
_topBatX =_screenWidth/2-10;
_bottomBatX =_screenWidth/2-10;
}
if (_toppoint==9){
}
else if (_bottompoint==9) {
}
if(_ballX > _screenWidth || _ballX <= 40){
_ballVelocityX *= -1;
}
if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY < _topBatY){
_ballVelocityY *= -1;
}
if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength && _ballY > _bottomBatY){
_ballVelocityY *= -1;
}
if (_ballX == 40+_batLength) {
_topBatX=40;
} else if (_ballX > 40+_batLength && _ballX <= _screenWidth){
_topBatX=_ballX-_batLength+6;
}
if (_bottomBatX < 46) {
_bottomBatX=46;
} else if (_bottomBatX+_batLength > _screenWidth){
_bottomBatX=_screenWidth-_batLength+6;
}
}
public void draw(Canvas c, Paint paint) {
paint.setARGB(255, 234, 222, 199);
c.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), paint);
c.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, _topBatY + _batHeight), paint); //top bat
c.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, _bottomBatY + _batHeight), paint); //bottom bat
}
}
私のXMLファイルでは:
<ImageView
android:id="@+id/counterT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/counter00"
android:layout_gravity="left|center_vertical"
/>
<ImageView
android:id="@+id/counterB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/counter00" />
誰でも助けることができますか?