-1

The issue is that when I click the button the app force closes and gives an error. The app is a memory game. When I click the button it is suppose to go to the game page.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memory_game);

    GridLayout gridLayout = (GridLayout) 
findViewById(R.id.grid_layout_4x4);

    int numColumns = gridLayout.getColumnCount();
    int numRows = gridLayout.getRowCount();

    numberOfElements = numColumns +numRows;

    buttons = new MemoryButtons[numberOfElements];

    buttonGraphics = new int[numberOfElements/ 2];

    buttonGraphics[0] = R.drawable.button_1;
    buttonGraphics[1] = R.drawable.button_2;
    buttonGraphics[2] = R.drawable.button_3;
    buttonGraphics[3] = R.drawable.button_4;
    buttonGraphics[4] = R.drawable.button_5;
    buttonGraphics[5] = R.drawable.button_6;
    buttonGraphics[7] = R.drawable.button_7;
    buttonGraphics[8] = R.drawable.button_8;

    buttonGraphicLocations = new int[numberOfElements];

    shuffleButtonGraphics();

    for (int r = 0; r < numRows; r++){
        for (int c = 0; c < numColumns; c++){
            MemoryButtons tempButton = new 
MemoryButtons(this,r,c,buttonGraphics[buttonGraphicLocations[r* 
numColumns * c]]);
            tempButton.setId(View.generateViewId());
            buttons[r * numColumns *c] = tempButton;
            gridLayout.addView(tempButton);
        }
    }

}

protected void shuffleButtonGraphics(){
    Random rand = new Random();

    for(int i = 0; i < numberOfElements; i++){
        buttonGraphicLocations[i] = i% (numberOfElements / 2);
    }

    for (int i = 0; i < numberOfElements; i++){
        int temp = buttonGraphicLocations[i];
        int swapIndex = rand.nextInt(16);
        buttonGraphicLocations[i] = buttonGraphicLocations[swapIndex];
        buttonGraphicLocations[swapIndex]  = temp;
    }
}


ERROR:
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.game/com.example.game.MemoryGame}: 
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)
    at android.app.ActivityThread.access$1100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5601)
    at java.lang.reflect.Method.invoke(Native Method)    atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 at com.example.game.MemoryGame.onCreate(MemoryGame.java:46)

Zoe
  • 23,712
  • 16
  • 99
  • 132

1 Answers1

0

Reason for crash is rand.nextInt(16);

You generate random number between 0-16 but your actual buttonGraphicLocations size is 8.

Solution is

You should use numberOfElements = numColumns * numRows; instead of numberOfElements = numColumns + numRows;

Mohanraj
  • 176
  • 13