我是 android 开发的新手,我正在尝试编写一个简单的类似 Simon 的游戏来进行一些练习,但是我已经变得非常卡住,并且希望得到任何关于它为什么不运行以及我可以做些什么来修复它的帮助:
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);
}
}
}
一般的想法是,它用随机数填充 ArrayList,然后通过将按钮的颜色从较深的颜色更改为较浅的颜色来播放此序列。然后激活按钮并将点击记录到第二个 ArrayList 中,并将其与原始的比较,以检查玩家是否输入了正确的组合。
然而,它在加载时崩溃,留下以下调试信息。然而,由于我的理解有限,事实证明它太神秘而无法定位问题:
10-17 17:12:10.758: D/AndroidRuntime(10439): 关闭 VM 10-17 17:12:10.758: W/dalvikvm(10439): threadid=1: 线程退出未捕获异常 (group=0x40c47300) 10 -17 17:12:10.768:E/AndroidRuntime(10439):致命异常:主要 10-17 17:12:10.768:E/AndroidRuntime(10439):java.lang.RuntimeException:无法启动活动 ComponentInfo{joc.simondroid /joc.simondroid.SimonActivity}:java.lang.IndexOutOfBoundsException:无效索引 0,大小为 0 10-17 17:12:10.768:E/AndroidRuntime(10439):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2059) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在android.app.ActivityThread.access$600(ActivityThread.java:130) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 android.os.Handler.dispatchMessage (Handler.java:99) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 android.os.Looper.loop(Looper.java:137) 10-17 17:12:10.768: E/AndroidRuntime (10439): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 java.lang.reflect.Method.invokeNative(Native Method) 10 -17 17:12:10.768:E/AndroidRuntime(10439):在 java.lang.reflect.Method.invoke(Method.java:511) 10-17 17:12:10.768:E/AndroidRuntime(10439):在 com .android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 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) ): 原因: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): 在 java.util.ArrayList.get(ArrayList.java:304) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 joc.simondroid .SimonActivity.onCreate(SimonActivity.java:99) 10-17 17:12:10.768: E/AndroidRuntime(10439): 在 android.app.Activity.performCreate(Activity.java:5008) 10-17 17:12:10.768 : E/AndroidRuntime(10439): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 10-17 17:12:10.768:E/AndroidRuntime(10439): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 10-17 17:12:10.768: E/AndroidRuntime(10439): ... 11 更多