You create an anonymous class ( View.OnClickListener ) for each button, the onClick() method in this class has a different scope than the getValuesPressed() method, so it does not have access to the local variable i .
The solution is in the link above:
An anonymous class cannot access local variables in its application that are not declared final or actually final.
Therefore, introducing the final variable into the loop would eliminate the error:
String getValuesPressed(){ for(int i = 0; i < buttonList.length; i++){ final int j = i; buttonList[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(j == 0){
source share