Reputation: 160
I am not sure why this code doesn't work. I am trying to make a choose your own adventure game. The text describing your situation are text views, yet I cannot set them up. Android Studio says that findViewByID() cannot be resolved.
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);
}
}
}
I know that this isn't a lot of code, so if you need more context, I am willing to copy paste my entire code into here. I also have made sure that TextView is imported.
Upvotes: 0
Views: 329
Reputation: 6071
You'll need to call findViewById(int)
in your onCreate(Bundle)
method, after setting the content view (via setContentView(int)
).
Otherwise you'll possibly get a NullPointerException
down the road due to the fact that the views haven't been created when the variables are set.
Upvotes: 3