在 Android Java 编程中使用枚举
Using enum in Android Java programming
我正在尝试用 android 编写游戏,我想使用枚举。
这是我的枚举 class:
public enum State {
INACTIVE(0), ACTIVE(1);
private final int value;
private State(int value){
this.value = value;
}
public int getValue() {
return value;
}
}
然后我想在我的方法和构造函数中使用枚举:
public FieldsGame(){
Board = new char[BOARD_SIZE_VERTICAL][BOARD_SIZE_HORIZONTAL];
for (int i = 0; i < BOARD_SIZE_VERTICAL; i++) {
for (int j = 0; j < BOARD_SIZE_HORIZONTAL; j++) {
Board[i][j] = EMPTY_SPACE;
BoardState[i][j] = State.INACTIVE.getValue();
}
}
}
public void clearBoard() {
for (int i = 0; i < BOARD_SIZE_VERTICAL; i++) {
for (int j = 0; j < BOARD_SIZE_HORIZONTAL; j++) {
Board[i][j] = EMPTY_SPACE;
BoardState[i][j] = State.INACTIVE.getValue();
}
}
}
当我尝试启动我的应用程序时,它说 "Unfortunatelly, application has stopped."
当我评论这两行时,我确定这是因为枚举原因:
BoardState[i][j] = State.INACTIVE.getValue();
在我使用枚举的地方,它可以工作。有人可以帮帮我吗?
When I'm trying to launch my application it says "Unfortunately,
application has stopped." I'm sure it is because of enum because when I
comment these two lines where I'm using enum, it works. Can somebody
help me, please?
then 可能是因为 BoardState
从未初始化,而您正在尝试访问空对象。添加
BoardState = new int[BOARD_SIZE_VERTICAL][BOARD_SIZE_HORIZONTAL];
在 FieldsGame
中的 for 循环之前。
我正在尝试用 android 编写游戏,我想使用枚举。
这是我的枚举 class:
public enum State {
INACTIVE(0), ACTIVE(1);
private final int value;
private State(int value){
this.value = value;
}
public int getValue() {
return value;
}
}
然后我想在我的方法和构造函数中使用枚举:
public FieldsGame(){
Board = new char[BOARD_SIZE_VERTICAL][BOARD_SIZE_HORIZONTAL];
for (int i = 0; i < BOARD_SIZE_VERTICAL; i++) {
for (int j = 0; j < BOARD_SIZE_HORIZONTAL; j++) {
Board[i][j] = EMPTY_SPACE;
BoardState[i][j] = State.INACTIVE.getValue();
}
}
}
public void clearBoard() {
for (int i = 0; i < BOARD_SIZE_VERTICAL; i++) {
for (int j = 0; j < BOARD_SIZE_HORIZONTAL; j++) {
Board[i][j] = EMPTY_SPACE;
BoardState[i][j] = State.INACTIVE.getValue();
}
}
}
当我尝试启动我的应用程序时,它说 "Unfortunatelly, application has stopped." 当我评论这两行时,我确定这是因为枚举原因:
BoardState[i][j] = State.INACTIVE.getValue();
在我使用枚举的地方,它可以工作。有人可以帮帮我吗?
When I'm trying to launch my application it says "Unfortunately, application has stopped." I'm sure it is because of enum because when I comment these two lines where I'm using enum, it works. Can somebody help me, please?
then 可能是因为 BoardState
从未初始化,而您正在尝试访问空对象。添加
BoardState = new int[BOARD_SIZE_VERTICAL][BOARD_SIZE_HORIZONTAL];
在 FieldsGame
中的 for 循环之前。