Android游戏-致命例外:线程87

时间:2022-12-27 21:11:52

I programmed an Android game via this Youtube tutorial, there you can see how the game should look like at the end.

我通过这个Youtube教程编写了一个Android游戏,在那里你可以看到游戏最后的样子。

Everything went fine until I added the Gameover Screen. When I start the emulator, the Game runs accordingly, but when I have used up all my lives and the View should change to the Gameover Screen, it only appears for 1 second and then crashes (the game exits).

一切都很顺利,直到我在屏幕上添加了游戏。当我启动仿真器时,游戏会相应地运行,但是当我用尽了所有的生命并且视图应该改变为Gameover屏幕时,它只出现了1秒,然后就崩溃了(游戏退出)。

This is what the LogCat says:

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)

Here is my GameOverActivity class

这是我的游戏过动类

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()));
}

}

Here is my GameActivity class

这是我的游戏活动课程

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());
    }
}

}

and here my GameView class

这是我的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;
}
}

I am a beginner so I really would appreciate some help.

我是个初学者,所以我很感激你的帮助。

Edit:

编辑:

Don't create new variables inside the onDraw statement.

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

So the variables like yTextPos (int) or fontSize(int) from the draw-method should be created above with all the other global variables? Isn't this make it more confusing? Or what are the benefits?

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

Also, what is line 79 in GameView.java?

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

Do you mean

你的意思是

canvas.drawColor(Color.DKGRAY);

? This gives the background of the game the color DKGRAY. I don't have a specific image for the background yet.

吗?这给了游戏的背景颜色DKGRAY。我还没有具体的背景图像。

1 个解决方案

#1


0  

I believe you should set your game running loop to false when you are in the game over state. You might need to join or something as well.

我认为你应该将游戏运行循环设置为false。你可能也需要加入。

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

#1


0  

I believe you should set your game running loop to false when you are in the game over state. You might need to join or something as well.

我认为你应该将游戏运行循环设置为false。你可能也需要加入。

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