Basically I have to make a maths game with easy/medium/hard difficulties. I used the switch statement to achieve this which calls a method that deals with what to do in each difficulty.
The problem is that I can’t get my head around what this method with the switch statement should return, if anything, and how should I call it in the onCreate method. Also I don’t know why, but the TextView in the getPuzzle method is giving me an error when I call the tv.setText method on it.
public class Game extends Activity {
private static final String TAG = "Brain Training";
public static final String KEY_DIFFICULTY = "com.coursework.braintrain.difficulty";
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
public static final int DIFFICULTY_GURU = 3;
private int brain;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
setBrain(getPuzzle(diff));
getBrain();
setContentView(R.layout.gamelayout);
final EditText edittext = (EditText) findViewById(R.id.USERentry);
final Button button1 = (Button) findViewById(R.id.keypad_1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks
//String buttonText = (String) button.getText();
edittext.setText(edittext.getText() + "1");
//edittext.setText() = edittext.getText() + "1";
}
});
//rest of the bottons...
}
public void Easy12(){
int a = (int) Math.random();
int b = (int) Math.random();
final TextView tv = (TextView) findViewById(R.id.question_label);
String aString = Integer.toString(a);
String bString = Integer.toString(b);
String display = aString + bString;
tv.setText(display);
}
private int getPuzzle(int diff){
//TODO: Continue last game
switch(diff){
case DIFFICULTY_HARD:
break;
case DIFFICULTY_MEDIUM:
break;
case DIFFICULTY_EASY:
Easy12();
break;
default: Easy12();
}
//might require to return an array or something
return diff;
}
public int getBrain() {
return brain;
}
public void setBrain(int brain) {
this.brain = brain;
}
}
Any advice would be appreciated.
EDIT: Here’s the xml file that goes along with it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
<EditText
android:id="@+id/USERentry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textColor="@color/user_txt_color"/>"
<TableLayout
android:id="@+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<Button android:id="@+id/keypad_1"
android:text="1">
</Button>
<Button android:id="@+id/keypad_2"
android:text="2"
android:onClick="">
</Button>
<Button android:id="@+id/keypad_3"
android:text="3">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_4"
android:text="4">
</Button>
<Button android:id="@+id/keypad_5"
android:text="5">
</Button>
<Button android:id="@+id/keypad_6"
android:text="6">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_7"
android:text="7">
</Button>
<Button android:id="@+id/keypad_8"
android:text="8">
</Button>
<Button android:id="@+id/keypad_9"
android:text="9">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_0"
android:text="0">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_del"
android:text="del">
</Button>
<Button android:id="@+id/keypad_minus"
android:text="-">
</Button>
<Button android:id="@+id/keypad_hash"
android:text="#">
</Button>
</TableRow>
</TableLayout>
</LinearLayout>
You are getting a null pointer exception in the
Easy12()method. As suggested by Knossos, this looks likely to be caused byquestion_labelnot existing in your xml layout. Make sure your xml contains something that looks like this:You can still change the text of a TextView at runtime with the
setText()method, theandroid:textattribute is just an initial value.If you still can’t get it working or see the problem, post your xml layout as well. If this was the problem, you might want to consider reading http://developer.android.com/guide/topics/ui/declaring-layout.html to understand xml layouts and how to use them a bit more.