Android游戏 - 致命异常:Thread-87

时间:2014-04-05 10:08:45

标签: android multithreading nullpointerexception

我通过this Youtube教程编写了一款Android游戏,在那里你可以看到游戏最终应该是什么样子。

在我添加Gameover Screen之前,一切都很顺利。当我启动模拟器时,游戏会相应地运行,但是当我已经用完了所有生命并且视图应该更改为Gameover屏幕时,它只会显示1秒然后崩溃(游戏退出)。

这就是LogCat所说的:

04-05 06:14:22.178: E/AndroidRuntime(1201): FATAL EXCEPTION: Thread-87
04-05 06:14:22.178: E/AndroidRuntime(1201): Process: com.skies.game, PID: 1201
04-05 06:14:22.178: E/AndroidRuntime(1201): java.lang.NullPointerException
04-05 06:14:22.178: E/AndroidRuntime(1201): at com.skies.game.GameView.draw(GameView.java:79)
04-05 06:14:22.178: E/AndroidRuntime(1201): at com.skies.game.GameLoopThread.run(GameLoopThread.java:30)

这是我的GameOverActivity类

package com.skies.game;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GameOverActivity extends Activity implements OnClickListener {

private Button bReplay;
private Button bExit;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameoverscreen);
    bReplay = (Button) findViewById(R.id.bReplay);
    bExit = (Button) findViewById(R.id.bExit);
    bReplay.setOnClickListener(this);
    bExit.setOnClickListener(this);
    initialize();
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bReplay:
        Intent newGameScreen = new Intent(this, GameActivity.class);
        startActivity(newGameScreen); 
        this.finish(); 
        break;
    case R.id.bExit:
        this.finish();
        break;
    }

}

public int readHighscore() {
    SharedPreferences pref = getSharedPreferences("GAME", 0);
    return pref.getInt("HIGHSCORE", 0);
}

public void initialize() {
    int score = this.getIntent().getExtras().getInt("score");
    TextView tvScore = (TextView) findViewById(R.id.tvScore);
    tvScore.setText("Your score is: " + Integer.toString(score));
    TextView tvHighscore = (TextView) findViewById(R.id.tvHighScore);
    tvHighscore.setText("Endless Game Highscore: "
            + Integer.toString(readHighscore()));
}

}

这是我的GameActivity类

package com.skies.game;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;

public class GameActivity extends Activity {

private GameView theGameView;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    theGameView = new GameView(this);
    setContentView(theGameView);  
}

public void onGameOver()
{
    compareScore();
    Intent theNextIntent = new Intent (getApplicationContext(),  GameOverActivity.class);
    theNextIntent.putExtra("score", theGameView.getScore());
    startActivity(theNextIntent);
    this.finish();
}

public int readHighscore()
{
    SharedPreferences pref = getSharedPreferences("GAME", 0);
    return pref.getInt("HIGHSCORE", 0);
}

public void writeHighscore(int highscore)
{
    SharedPreferences pref = getSharedPreferences("GAME", 0);
    SharedPreferences.Editor editor = pref.edit();  
    editor.putInt("HIGHSCORE", highscore);      
    editor.commit();                                
}

public void compareScore()
{
    if(theGameView.getScore() > readHighscore())
    {
        writeHighscore(theGameView.getScore());
    }
}

}

这里是我的GameView类

package com.skies.game;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GameView extends SurfaceView {

private List<Sprite> spriteList = new ArrayList<Sprite>();
private List<Integer> spriteListNum = new ArrayList<Integer>();
private SurfaceHolder surfaceHolder;
private Bitmap bmp;
private Bitmap livesPicture;
private GameLoopThread theGameLoopThread;
private boolean createSprites = true;
private long lastClick;
private int currentColorNum; 
private int lives = 4;
private int score = 0;
private Paint paintRed, paintBlue, paintGreen, paintYellow;
private Paint currentColor;
private String scoreString;
private String livesString;
private float density;
private GameActivity theGameActivity= new GameActivity();

public GameView(Context context) {
    super(context);
    livesPicture = BitmapFactory.decodeResource(getResources(),
            R.drawable.lives);
    Random rnd = new Random();
    theGameActivity = (GameActivity) context;
    setColors();
    currentColorNum = rnd.nextInt(4);  
    theGameLoopThread = new GameLoopThread(this);
    surfaceHolder = getHolder();
    surfaceHolder.addCallback(new SurfaceHolder.Callback() {

        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            theGameLoopThread.setRunning(false);
            while (retry) {
                try {
                    theGameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {

                }
            }

        }

        public void surfaceCreated(SurfaceHolder holder) {
            theGameLoopThread.setRunning(true);
            theGameLoopThread.start();
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {


        }
    });
}

@Override
public void draw(Canvas canvas) {
    canvas.drawColor(Color.DKGRAY);
    if (createSprites == true) {
        initialSprites();
    }
    for (Sprite sprite : spriteList) {
        sprite.draw(canvas);
    }
    if (currentColorNum == 0) {         
        drawLines(paintBlue, canvas);
    } else if (currentColorNum == 1) {
        drawLines(paintRed, canvas);
    } else if (currentColorNum == 2) {
        drawLines(paintGreen, canvas);
    } else if (currentColorNum == 3) {
        drawLines(paintYellow, canvas);
    }
    final int fontSize = (int) (25 * density);
    int yTextPos = (int) (25 * density);
    Typeface font = Typeface.create("Arial", Typeface.NORMAL);

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setTypeface(font);
    paint.setTextSize(fontSize);
    paint.setAntiAlias(true); 
    scoreString = String.valueOf(score);
    int x = (canvas.getWidth() * 5 / 7);
    final String text = "Score: " + scoreString;
    canvas.drawText(text, x, yTextPos, paint);
    drawLives(canvas, paint);
}

private void createSprite(int index) {
    Bitmap bmp = null;
    switch (index) {
    case 0:
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.alienspriteblue);
        break;
    case 1:
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.alienspritered);
        break;
    case 2:
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.alienspritegreen);
        break;
    case 3:
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.alienspriteyellow);
        break;
    }
    Sprite sprite = new Sprite(this, bmp);
    spriteList.add(sprite);
    spriteListNum.add(index);
}

private void initialSprites() {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++)
            createSprite(i);
    }
    createSprites = false;
}

private void rndCreateSprite() {
    Random rnd = new Random(System.currentTimeMillis());
    int i = rnd.nextInt(4);
    createSprite(i);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 300) {
        lastClick = System.currentTimeMillis();
        synchronized (getHolder()) {
            for (int i = spriteList.size() - 1; i >= 0; i--) {
                Sprite sprite = spriteList.get(i);
                if (sprite.isTouched(event.getX(), event.getY())) {
                    if (currentColorNum == spriteListNum.get(i)) {
                        score++;
                    }else{
                        lives--;
                        if(lives==0){
                        theGameActivity.onGameOver();
                        }
                    }
                    if(score==3)
                        lives++;
                    rndCreateSprite();
                    removeSprite(i);
                    changeColor();
                    break;
                }
            }
        }
    }
    return true;
}

private void removeSprite(int index) {
    spriteList.remove(index);
    spriteListNum.remove(index);
}

public void setColors() {
    Paint paintRed = new Paint();
    paintRed.setARGB(255, 236, 27, 36); 
    this.paintRed = paintRed;
    Paint paintBlue = new Paint();
    paintBlue.setARGB(255, 36, 72, 204);
    this.paintBlue = paintBlue;
    Paint paintGreen = new Paint();
    paintGreen.setARGB(255, 34, 177, 76);
    this.paintGreen = paintGreen;
    Paint paintYellow = new Paint();
    paintYellow.setARGB(255, 255, 242, 0);
    this.paintYellow = paintYellow;
}

public void drawLines(Paint lineColor, Canvas canvas) {
    int lineWidth = (int) (10*density);
    int screenHeight = getHeight();
    int screenWidth = getWidth();
    canvas.drawRect(0, 0, lineWidth, getHeight(), lineColor);
    canvas.drawRect(0, getHeight() - lineWidth, screenWidth, screenHeight,
            lineColor);
    canvas.drawRect(screenWidth - lineWidth, 0, screenWidth, screenHeight,
            lineColor);
    currentColor = lineColor;
}

public void changeColor() {
    Random rnd = new Random();
    int index = rnd.nextInt(spriteListNum.size());
    this.currentColorNum = spriteListNum.get(index);
    switch (index) {
    case 0:
        currentColor = paintBlue;
        break;
    case 1:
        currentColor = paintRed;
        break;
    case 2:
        currentColor = paintGreen;
        break;
    case 3:
        currentColor = paintYellow;
        break;
    }

}

public float getDensity() {
    density = getResources().getDisplayMetrics().density;
    return density;
}

private void drawLives(Canvas canvas, Paint paint){
    int xHeart= (int) (15*density);
    int yHeart= (int) (12*density);
    if (lives == 3) {
        canvas.drawBitmap(livesPicture, xHeart,
                yHeart, paint);
        canvas.drawBitmap(livesPicture,
                xHeart + livesPicture.getWidth() + 3*density,
                yHeart, paint);
        canvas.drawBitmap(livesPicture, xHeart + 2
                * livesPicture.getWidth() + 6*density, yHeart, paint);
    }
    if (lives == 2) {
        canvas.drawBitmap(livesPicture, xHeart,
                yHeart, paint);
        canvas.drawBitmap(livesPicture,
                xHeart + livesPicture.getWidth() + 3,
                yHeart, paint);
    }
    if (lives == 1) {
        canvas.drawBitmap(livesPicture, xHeart,
                yHeart, paint);
    }
    if (lives > 3) {
        livesString = String.valueOf(lives);
        final String lives = livesString + "x";
        canvas.drawText(lives, 35 * getDensity(), 30 * getDensity(), paint);
        canvas.drawBitmap(livesPicture, 15 * getDensity() + 2
                * livesPicture.getWidth() + 6, 12 * getDensity(), paint);
    }
}

public int getScore() {
    return this.score;
}
}

我是初学者,所以我真的很感激一些帮助。

编辑:

  

不要在onDraw语句中创建新变量。

因此,应该使用所有其他全局变量在上面创建draw-method中的yTextPos(int)或fontSize(int)等变量?这不是让它更混乱吗?或者有什么好处?

  

另外,GameView.java第79行是什么?

你的意思是

canvas.drawColor(Color.DKGRAY);

?这给游戏的背景颜色DKGRAY。我还没有特定的背景图片。

1 个答案:

答案 0 :(得分:0)

我相信当你处于游戏结束状态时,你应该将游戏运行循环设置为false。你可能还需要加入或者其他东西。

theGameLoopThread.setRunning(true);
theGameActivity.onGameOver();