我正在从教程编写一个简单的Android游戏(参见上一篇文章)。我现在想要添加一个暂停屏幕,当按下后退按钮时会出现该屏幕。但是现在当我开始新游戏时游戏崩溃了。应用程序启动,菜单屏幕出现没有问题,但当我按“播放”时,屏幕变黑,应用程序崩溃。
这就是LogCat所说的:
04-07 09:23:04.759: E/AndroidRuntime(1134): FATAL EXCEPTION: main
04-07 09:23:04.759: E/AndroidRuntime(1134): Process: com.skies.game, PID: 1134
04-07 09:23:04.759: E/AndroidRuntime(1134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skies.game/com.skies.game.GameActivity}: java.lang.ClassCastException: com.skies.game.GameActivity cannot be cast to android.content.DialogInterface$OnDismissListener
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.os.Looper.loop(Looper.java:137)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-07 09:23:04.759: E/AndroidRuntime(1134): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 09:23:04.759: E/AndroidRuntime(1134): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 09:23:04.759: E/AndroidRuntime(1134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-07 09:23:04.759: E/AndroidRuntime(1134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-07 09:23:04.759: E/AndroidRuntime(1134): at dalvik.system.NativeStart.main(Native Method)
04-07 09:23:04.759: E/AndroidRuntime(1134): Caused by: java.lang.ClassCastException: com.skies.game.GameActivity cannot be cast to android.content.DialogInterface$OnDismissListener
04-07 09:23:04.759: E/AndroidRuntime(1134): at com.skies.game.GameActivity.onCreate(GameActivity.java:35)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.Activity.performCreate(Activity.java:5243)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 09:23:04.759: E/AndroidRuntime(1134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-07 09:23:04.759: E/AndroidRuntime(1134): ... 11 more
这是我的GameActivity类
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.SearchManager.OnDismissListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;
public class GameActivity extends Activity implements OnClickListener, OnDismissListener
{
private GameView theGameView;
private Button resume, menu, exit, newTry, pause;
private ToggleButton vibrationToggle, soundToggle;
private Dialog dialog;
private boolean firstStart = true;
private boolean dialogIsActive = false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
theGameView = new GameView(this);
setContentView(theGameView);
dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.pausescreen);
dialog.hide();
dialog.setOnDismissListener((android.content.DialogInterface.OnDismissListener) this);
initialize();
}
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);
(default)
}
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());
}
}
private void initialize()
{
resume = (Button) dialog.findViewById(R.id.bContinue);
menu = (Button) dialog.findViewById(R.id.bMainMenu);
newTry = (Button) dialog.findViewById(R.id.bNewTry);
resume.setOnClickListener(this);
menu.setOnClickListener(this);
newTry.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.bContinue:
dialog.hide();
dialogState();
break;
case R.id.bMainMenu:
compareScore();
dialog.dismiss();
Intent menuIntent = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(menuIntent);
finish();
break;
case R.id.bNewTry:
compareScore();
Intent newGameScreen = new Intent(this, GameActivity.class);
startActivity(newGameScreen);
dialog.dismiss();
finish();
}
}
protected void onDestroy() {
super.onDestroy();
}
protected void onPause() {
super.onPause();
theGameView.pauseThread();
firstStart = false;
}
@Override
protected void onResume() {
super.onResume();
if(!firstStart) {
dialog.show();
dialogIsActive = true;
}
}
@Override
public void onBackPressed() {
dialogState();
}
public void dialogState() {
if(dialogIsActive) {
dialog.hide();
dialogIsActive = false;
theGameView.resumeThread();
} else if (!dialogIsActive) {
theGameView.pauseThread();
dialog.show();
dialogIsActive = true;
}
}
public void onDismiss(DialogInterface dialog) {
dialogState();
}
@Override
public void onDismiss() {
}
}
这是我的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();
private Typeface font;
private Paint paint;
private boolean transparency;
public GameView(Context context) {
super(context);
paint = new Paint();
font = Typeface.create("Arial", Typeface.NORMAL);
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.interrupt();
theGameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
theGameLoopThread.setRunning(true);
if(theGameLoopThread.getState() == theGameLoopThread
.getState().NEW) {
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);
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){
theGameLoopThread.setRunning(false);
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;
}
public void pauseThread() {
theGameLoopThread.setRunning(false);
}
public void resumeThread() {
theGameLoopThread = new GameLoopThread(this);
theGameLoopThread.setRunning(true);
theGameLoopThread.start();
}
}
我猜我在onDismissListener上犯了一些错误。我没有添加很多代码来实现Pausescreen,但我找不到错误。
提前致谢!
答案 0 :(得分:0)
您已在Activity类中导入(并实施)了错误的onDismissListener
。
在GameActivity
课程的顶部,更改
import android.app.SearchManager.OnDismissListener;
到
import android.content.DialogInterface.OnDismissListener;