I’m new to android development and I’m trying to code a simple Simon-like game to get some practice, I however have become quite stuck and would love any help regarding why it does not run and what I can do to fix it:
public class SimonActivity extends Activity {
ArrayList<Integer> table1 = randomArrayList(3,4);
ArrayList<Integer> table2 = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simon);
final Button blue = (Button)findViewById(R.id.blueButton);
final Button green = (Button)findViewById(R.id.greenButton);
final Button gold = (Button)findViewById(R.id.goldButton);
final Button red = (Button)findViewById(R.id.redButton);
//turn off the buttons
activate(blue, green, gold, red, false);
final Handler handler = new Handler();
final Runnable blueLight = new Runnable() {
public void run() {
blue.setBackgroundResource(R.drawable.lblue);
}
};
final Runnable goldLight = new Runnable() {
public void run() {
gold.setBackgroundResource(R.drawable.lgold);
}
};
final Runnable redLight = new Runnable() {
public void run() {
red.setBackgroundResource(R.drawable.lred);
}
};
final Runnable greenLight = new Runnable() {
public void run() {
green.setBackgroundResource(R.drawable.lgreen);
}
};
final Runnable switchOff = new Runnable() {
public void run() {
activate(blue, green, gold, red, false);
}
};
final Runnable switchOn = new Runnable() {
public void run() {
activate(blue, green, gold, red, true);
}
};
final Runnable playSequence = new Runnable() {
public void run() {
SystemClock.sleep(1000);
for (int i = 0; i < table1.size(); i++ ) {
switch(table1.get(i)) {
case 0 : handler.post(blueLight);
break;
case 1 : handler.post(goldLight);
break;
case 2 : handler.post(redLight);
break;
case 3 : handler.post(greenLight);
break;
}
SystemClock.sleep(700);
handler.post(switchOff);
SystemClock.sleep(300);
}
table2.clear();
handler.post(switchOn);
}
};
new Thread(playSequence).start();
while (true) {
if (table2.get(0) == 5){
table2.clear();
new Thread(playSequence).start();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_simon, menu);
return true;
}
public void activate(Button bt1, Button bt2, Button bt3, Button bt4, boolean valor) {
bt1.setClickable(valor);
bt2.setClickable(valor);
bt3.setClickable(valor);
bt4.setClickable(valor);
bt1.setBackgroundResource(R.drawable.bblue);
bt2.setBackgroundResource(R.drawable.bgreen);
bt3.setBackgroundResource(R.drawable.bgold);
bt4.setBackgroundResource(R.drawable.bred);
}
public static int random(int n) { //la de tota la vida
Random r = new Random();
int x = r.nextInt(n);
return x;
}
public static int[] randomarray(int tamany, int range) {
Random r = new Random();
int[] a = new int[tamany];
for (int i = 0; i < a.length; i++) {
a[i] = r.nextInt(range);
}
return a;
}
public static ArrayList<Integer> randomArrayList(int tamany, int range) {
Random r = new Random();
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < tamany; i++) {
a.add(r.nextInt(range));
}
return a;
}
public void clic(View target) {
switch (target.getId()) {
case R.id.blueButton: table2.add(0);
break;
case R.id.goldButton: table2.add(1);
break;
case R.id.redButton: table2.add(2);
break;
case R.id.greenButton: table2.add(3);
break;
}
for (int i = 0; i < table2.size(); i++ ) {
if (table1.get(i) != table2.get(i)) {
table2.clear();
table1.clear();
table1 = randomArrayList(3, 4);
table2.add(5);
break;
}
}
if (table1.equals(table2)) {
table2.clear();
table1.add(random(4));
table2.add(5);
}
}
}
The general idea is that it fills an ArrayList with random numbers and then plays this sequence by changing the colors of the buttons from darker ones to lighter ones. Then activates the buttons and logs the clicks into a second ArrayList and compares it with the original one to check if the player has entered the correct combination.
It however crashes upon loading, leaving the following debug information. It has however, because of my limited understanding proved too cryptic to locate the problem:
10-17 17:12:10.758: D/AndroidRuntime(10439): Shutting down VM 10-17
17:12:10.758: W/dalvikvm(10439): threadid=1: thread exiting with
uncaught exception (group=0x40c47300) 10-17 17:12:10.768:
E/AndroidRuntime(10439): FATAL EXCEPTION: main 10-17 17:12:10.768:
E/AndroidRuntime(10439): java.lang.RuntimeException: Unable to start
activity ComponentInfo{joc.simondroid/joc.simondroid.SimonActivity}:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 10-17
17:12:10.768: E/AndroidRuntime(10439): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
android.app.ActivityThread.access$600(ActivityThread.java:130) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
android.os.Handler.dispatchMessage(Handler.java:99) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
android.os.Looper.loop(Looper.java:137) 10-17 17:12:10.768:
E/AndroidRuntime(10439): at
android.app.ActivityThread.main(ActivityThread.java:4745) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
java.lang.reflect.Method.invokeNative(Native Method) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
java.lang.reflect.Method.invoke(Method.java:511) 10-17 17:12:10.768:
E/AndroidRuntime(10439): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
dalvik.system.NativeStart.main(Native Method) 10-17 17:12:10.768:
E/AndroidRuntime(10439): Caused by:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 10-17
17:12:10.768: E/AndroidRuntime(10439): at
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
java.util.ArrayList.get(ArrayList.java:304) 10-17 17:12:10.768:
E/AndroidRuntime(10439): at
joc.simondroid.SimonActivity.onCreate(SimonActivity.java:99) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
android.app.Activity.performCreate(Activity.java:5008) 10-17
17:12:10.768: E/AndroidRuntime(10439): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-17 17:12:10.768: E/AndroidRuntime(10439): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-17 17:12:10.768: E/AndroidRuntime(10439): … 11 more
You are trying to access the first entry (index
0) of anArrayList. Since I don’t know the code lines (it is in theonCreatemethod though), I can only guess that it is this line:table2seems to be empty.