所以我有一个我正在写的Android Tetris游戏。当我在模拟器中运行应用程序并单击开始按钮时它崩溃了。没有一个对话框显示应用程序已停止运行(通常在抛出异常时得到它,对吧?)。而且我不知道哪行代码会引发异常,因为我无法看到堆栈跟踪!有谁知道崩溃报告的位置或我如何看到堆栈跟踪?我是android的初学程序员,我曾经写过WinForms应用程序。
我只展示了部分代码。如果您认为问题出在其他地方,请告诉我,以便我可以在这里发布。
顺便说一句,如果我犯了任何语法错误,对不起我的英语不好,因为我是中国人。
这是代码: 开始按钮:
<Button
android:layout_width="319dp"
android:layout_height="160dp"
android:text="START"
android:id="@+id/buttonStart"
android:layout_x="8dp"
android:layout_y="116dp"
android:textSize="30pt"
android:onClick="btnStartClick"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
OnClick事件处理程序:
public void btnStartClick (View view) {
startActivity (new Intent (StartPage.this, TetrisActivity.class));
}
TetrisActivity的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_tetris);
board = new TetrisBoard (this);//board is a TetrisBoard instance
board.addTetrimino ();
}
TetrisBoard课程
public class TetrisBoard implements ITetrisEventListener{
private TetrisActivity activity;
private Random r;
public TetrisBlock[][] tetrisBlockMatrix;
public TetrisBoard (TetrisActivity activity) {
tetrisBlockMatrix = new TetrisBlock[16][22];
r = new Random ();
this.activity = activity;
}
public void addTetrimino () {
Tetrimino tetrimino = null;
int randomNumber = r.nextInt (140);
if (randomNumber < 20) {
tetrimino = new OShapedTetrimino (tetrisBlockMatrix, activity);
} else if (randomNumber < 40) {
tetrimino = new IShapedTetrimino (r.nextInt (2), tetrisBlockMatrix,
activity);
} else if (randomNumber < 60) {
tetrimino = new ZShapedTetrimino (r.nextInt (2), tetrisBlockMatrix,
activity);
} else if (randomNumber < 80) {
tetrimino = new SShapedTetrimino (r.nextInt (2), tetrisBlockMatrix,
activity);
} else if (randomNumber < 100) {
tetrimino = new TShapedTetrimino (r.nextInt (4), tetrisBlockMatrix,
activity);
} else if (randomNumber < 120) {
tetrimino = new JShapedTetrimino (r.nextInt (4), tetrisBlockMatrix,
activity);
} else {
tetrimino = new LShapedTetrimino (r.nextInt (4), tetrisBlockMatrix,
activity);
}
tetrimino.registerLandedListeners (this);
tetrimino.setTimerInterval (activity.timerInterval);
activity.fallingTetrimino = tetrimino;
}
@Override
public void onLanded(Tetrimino sender) {
try {
sender.addTetrisBlocksToMatrix (tetrisBlockMatrix);
activity.checkScore ();
addTetrimino ();
} catch (IllegalArgumentException e) {
TetrisUtility.showMsgDialog ("Game Over", "Tetris", activity);
activity.finish ();
}
}
}
最后是Tetrimino课程:
public abstract class Tetrimino {
public TetrisBlock[] blocks;
protected Timer timer;
protected TetrisBlock[][] tetrisBlockMatrix;
protected ArrayList<ITetrisEventListener> landedEventListeners;
protected TimerTask task = new TimerTask () {
@Override
public void run() {
moveDown ();
}
};
public static final int LEFT = -1;
public static final int RIGHT = 1;
protected abstract TetrisBlock[] getTouchingSides();
protected void landed () {
for (int i = 0 ; i < landedEventListeners.size () ; i++) {
landedEventListeners.get (i).onLanded (this);
}
}
public void registerLandedListeners (ITetrisEventListener listener) {
landedEventListeners.add (listener);
}
public void moveDown () {
if (!checkLanded ()) {
for (TetrisBlock block:blocks) {
block.moveDown ();
}
} else {
timer.cancel ();
landed ();
}
}
protected boolean checkLanded () {
TetrisBlock[] touchingSides = getTouchingSides ();
for (TetrisBlock block:touchingSides) {
if (block.getY () >= 21) {
return true;
}
if (tetrisBlockMatrix[block.getX ()][block.getY () + 1] != null) {
return true;
}
}
return false;
}
public boolean checkPositionValid (int x, int y) {
if (x < 0 || y < 0 ||
x > 15 || y > 21)
return false;
if (tetrisBlockMatrix[x][y] == null)
return true;
return false;
}
public void move (int side) {
if (side == 1 || side == -1) {
for (TetrisBlock block:blocks) {
block.setX (block.getX () + side);
}
for (TetrisBlock block:blocks) {
if (!checkPositionValid (block.getX (), block.getY ())) {
if (side == LEFT)
move (RIGHT);
else
move (LEFT);
}
}
} else {
throw new IllegalArgumentException ();
}
}
public void addTetrisBlocksToMatrix (TetrisBlock[][] matrix) {
for (TetrisBlock block:blocks) {
if (matrix[block.getX ()][block.getY ()] == null) {
matrix[block.getX ()][block.getY ()] = block;
} else {
throw new IllegalArgumentException ();
}
}
}
public void setTimerEnabled (boolean value) {
if (value) {
timer.cancel ();
timer = new Timer ();
timer.scheduleAtFixedRate (task, 0L, 1000L);
} else {
timer.cancel ();
}
}
public void setTimerInterval (int milliseconds) {
timer.cancel ();
timer = new Timer ();
timer.scheduleAtFixedRate (task, 0L, milliseconds);
}
protected Tetrimino (TetrisBlock[][] matrix, TetrisActivity activity) {
this.tetrisBlockMatrix = matrix;
timer = new Timer ();
timer.scheduleAtFixedRate (task, 0L, 1000L);
landedEventListeners = new ArrayList<> ();
blocks = new TetrisBlock[4];
}
}
答案 0 :(得分:3)
我通过反复循环执行以下操作来修复它: