我无法弄清楚为什么这个应用程序会破坏

时间:2012-10-17 15:38:57

标签: java android

我是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中,并将其与原始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):致命异常:主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)   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):引起:   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更多

2 个答案:

答案 0 :(得分:2)

你做

if (table2.get(0)

但是之前您将其初始化为

ArrayList<Integer> table2 = new ArrayList<Integer>();

并且不要在其中放置任何项目,因此即使索引0也无法提供。 所以你得到IndexOutOfBoundsException

答案 1 :(得分:2)

  

引起: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)

您正尝试访问0的第一个条目(索引ArrayList)。由于我不知道代码行(虽然它在onCreate方法中),我只能猜测它是这一行:

if (table2.get(0) == 5)

table2似乎是空的。