for (int i = 0; i < 9; i++)
{
b[i].setOnClickListener(
new OnClickListener()
{
public void onClick(View v)
{
justclicked(i);
}
}
);
}
I am attempting to put an action listener on nine buttons using a for loop. The code above is giving me an error. Is the error caused by the value of i not being visible? Thanks a lot, world-class experts @ Stack Overflow!!
No, it’s because
iisn’t final, which it must be to access from an anonymous inner class.Add
before
and then use
finalIinstead ofi:justclicked(finalI);.After that, think up a better name for
iandfinalI.