(**Updated**) Sorry if this is getting more and more like a guide. Is there any other site I should post this on?

I'm making a yatzy game and I'm on my way learning for loops. My method is similar to the top answer's in this question: 
[dynamic button onclick event inside for loop](https://stackoverflow.com/questions/10146028/dynamic-button-onclick-event-inside-for-loop)
Here's my code: (Yes I'm mixing languages, I'm sorry)

```
package se.balderskolan.yatzy;

    import java.util.Random;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;

    public class MainActivity extends Activity {

Button btKasta;
Random r = new Random();
int[] tarningar = new int[5];
int[] bilder = new int [6];
ImageButton [] img= new ImageButton[5];
boolean [] bool = new boolean[5];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btKasta = (Button)findViewById(R.id.btKasta);

    bilder [0]=R.drawable.b1; //Pictures of the dices
    bilder [1]=R.drawable.b2;
    bilder [2]=R.drawable.b3;
    bilder [3]=R.drawable.b4;
    bilder [4]=R.drawable.b5;
    bilder [5]=R.drawable.b6;

    img [0] = (ImageButton)findViewById(R.id.imgbt1); //The grafical dices
    img [1] = (ImageButton)findViewById(R.id.imgbt2);
    img [2] = (ImageButton)findViewById(R.id.imgbt3);
    img [3] = (ImageButton)findViewById(R.id.imgbt4);
    img [4] = (ImageButton)findViewById(R.id.imgbt5);

    //The die-buttons (save or not)
    img [0].setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(bool [0] == false) {
                        bool [0] = true;
                    }

                    else{
                        bool [0] = false;
                    }
                }
            }
            );

    img [1].setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(bool [1] == false) {
                        bool [1] = true;
                    }

                    else{
                        bool [1] = false;
                    }
                }
            }
            );

    img [2].setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(bool [2] == false) {
                        bool [2] = true;
                    }

                    else{
                        bool [2] = false;
                    }
                }
            }
            );

    img [3].setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(bool [3] == false) {
                        bool [3] = true;
                    }

                    else{
                        bool [3] = false;
                    }
                }
            }
            );

    img [4].setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(bool [4] == false) {
                        bool [4] = true;
                    }

                    else{
                        bool [4] = false;
                    }
                }
            }
            );

    /*I want to replace the code above with something like this below

    for(imgbt=0; imgbt<5; imgbt++){
        img [imgbt].setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if(bool [imgbt] == false) {
                            bool [imgbt] = true;
                        }
                        else{
                            bool [imgbt] = false;
                        }

                    }
                }
        );

    }
    */

  //The die-throwing button
    btKasta.setOnClickListener(             
        new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    for(int i=0; i<5; i++){
                        tarningar [i]=r.nextInt(6);

                        if (bool[i]==false){
                            img [i].setImageResource(bilder [tarningar[i]]);}
                        else{}
                    }                   
                }
            }
    );
}

    }

```

(I can add that I have succesfully had the bottons save dices using the code without the for-loop.)

After doing some research in my code I discovered the following about the button-for-loop:

* The value of "imgbt" is 5 at row

```
if(bool [imgbt] == false)

```

Which gives me the "ArrayIndexOutOfBoundsException"-error.
* All of the buttons always changes the bool-value of bool [5] (Which doesn't exist. I found this by increasing the nuber of arrays created up in the declaration)

If I declare

```
    int imgbt = 0

```

inside the for-loop eclipse gives me the error:

```
    Cannot refer to a non-final variable imgbt inside an inner class defined in a different method

```

(Can someone explain this error?)

So do you know any solution to my problem, or another way to work around it, please do not hesitate to awnser!