我不确定为什么这段代码不起作用。我正在尝试选择你自己的冒险游戏。描述您的情况的文本是文本视图,但我无法设置它们。 Android Studio表示无法解析findViewByID()。
package com.blogspot.darokrithia.dungeonfungeon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RoomActivity extends AppCompatActivity {
static int row = 1; //current row of room
static int column = 1; //current column of room
static int totalRows = 5; //total number of rows (including blank rooms)
static int totalColumns = 5; //total number of columns (including blank rooms)
static Layout dungeon = new Layout(totalRows,totalColumns); //Layout of this
static Room currentRoom = dungeon.getRoom(row,column); //The rooms the player is in
TextView roomText = (TextView) findViewByID(R.id.RoomDescriptionText); //What the room says
TextView aOption = (TextView) findViewByID(R.id.AOptionText); //What option a does right now
TextView bOption = (TextView) findViewByID(R.id.BOptionText); //What option b does right now
TextView cOption = (TextView) findViewByID(R.id.COptionText); //What option c does right now
TextView dOption = (TextView) findViewByID(R.id.DOptionText); //What option d does right now
@Override
protected void onCreate(Bundle savedInstanceState) { // No fucking idea what this does, but android studio seems to need it.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
currentRoom = dungeon.getRoom(row,column);
}
public void aButtonCLick(View v){
if (v.getId() == R.id.AButton){
dungeon.optionA(currentRoom);
}
}
public void bButtonCLick(View v){
if (v.getId() == R.id.BButton){
dungeon.optionB(currentRoom);
}
}
public void cButtonCLick(View v){
if (v.getId() == R.id.CButton){
dungeon.optionC(currentRoom);
}
}
public void dButtonCLick(View v){
if (v.getId() == R.id.DButton){
dungeon.optionD(currentRoom);
}
}
public Room getCurrentRoom(){
return currentRoom;
}
public static boolean canMove(int direction){ //makes sure there is no zero room (A wall / blank room) in the direction you want to go
Room testRoom;
switch (direction){
case 1:
testRoom = dungeon.getRoom((row-1),column);
if(testRoom.getRoomID() == 0){
return false;
}
else{
return true;
}
case 2:
testRoom = dungeon.getRoom(row,(column-1));
if(testRoom.getRoomID() == 0){
return false;
}
else{
return true;
}
case 3:
testRoom = dungeon.getRoom(row,(column+1));
if(testRoom.getRoomID() == 0){
return false;
}
else{
return true;
}
case 4:
testRoom = dungeon.getRoom((row+1),column);
if(testRoom.getRoomID() == 0){
return false;
}
else{
return true;
}
default:
return false;
}
}
public static void moveNorth(){ //moves up one row
if(canMove(1)){
row -= 1;
currentRoom = dungeon.getRoom(row, column);
}
}
public static void moveEast(){ //moves across one column
if(canMove(2)){
column -= 1;
currentRoom = dungeon.getRoom(row,column);
}
}
public static void moveWest(){ //moves back one column
if(canMove(3)){
column += 1;
currentRoom = dungeon.getRoom(row,column);
}
}
public static void moveSouth(){ //moves down one row
if(canMove(4)){
row += 1;
currentRoom = dungeon.getRoom(row,column);
}
}
}
我知道这不是很多代码,所以如果你需要更多的上下文,我愿意将我的整个代码粘贴到这里。我还确保导入TextView。
答案 0 :(得分:7)
findViewById()
最后有一个小写的“d”。
答案 1 :(得分:3)
在设置内容视图(通过findViewById(int)
)后,您需要在onCreate(Bundle)
方法中致电setContentView(int)
。
否则,由于在设置变量时尚未创建视图,因此您可能会获得NullPointerException
。