我是新來的android開發,我試圖編寫一個簡單的西蒙般的遊戲來得到一些練習,但我已經變得相當卡住,並會喜歡任何幫助爲什麼它不運行,我能做些什麼來解決這個問題:我不明白爲什麼這個應用程序中斷
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:線程退出 未捕獲的異常(組= 0x40c47300)10-17 17:12:10.768: E/AndroidRuntime(10439):致命異常:main 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) :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) E/AndroidRuntime(10439)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 :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):導致: java.lang.IndexOutOfBoundsException:索引0無效,大小爲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多個
非常感謝你,我瘋了。 – user1090729
@ user1090729不客氣。嘗試自己調查堆棧跟蹤。也許你下次可以發現錯誤;) – Baz