Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:01:24+00:00 2026-06-12T22:01:24+00:00

I’m new to android development and I’m trying to code a simple Simon-like game

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:01:25+00:00Added an answer on June 12, 2026 at 10:01 pm

    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)

    You are trying to access the first entry (index 0) of an ArrayList. Since I don’t know the code lines (it is in the onCreate method though), I can only guess that it is this line:

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

    table2 seems to be empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.