gpt4 book ai didi

java - Android 崩溃并且崩溃报告未显示在 Android Studio 控制台中

转载 作者:行者123 更新时间:2023-11-29 05:02:44 28 4
gpt4 key购买 nike

所以我正在编写一个 Android 俄罗斯方 block 游戏。当我在模拟器中运行该应用程序并单击开始按钮时,它崩溃了。没有一个对话框表明应用程序已停止运行(您通常会在抛出异常时看到它,对吧?)。而且我不知道是哪一行代码抛出异常,因为我看不到堆栈跟踪!有谁知道崩溃报告在哪里或如何查看堆栈跟踪?我是 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 ();

}

俄罗斯方 block 类

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];
}

最佳答案

我通过重复以下循环来修复它:

  • 重新启动 Logcat
  • 将过滤器从仅显示选定的应用程序更改为无过滤器
  • 将日志级别更改为调试并返回详细
  • 拔下并插回设备
  • 运行 adb kill-server 和 adb start-server
  • 重启 Android Studio
  • 重启电脑

关于java - Android 崩溃并且崩溃报告未显示在 Android Studio 控制台中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com