title: How access to a variable within inner class Android
body: <p>I've created programmatically 4 radio groups. The problem comes, when i try to use <code>setOnClickListener</code> through an inner class, that needs access to the iteration variable (i) of the loop used to declare the radio groups. I tried also to have the iterator variable final, but it didn't work. I need to set all 4 radio groups <code>clearCheck()</code>. Here is my code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      **RadioGroup[] radioGroup;**
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          radioGroup = new RadioGroup[4];
          for (int i = 0; i &lt; 4; i++) {
              radioGroup[i] = new RadioGroup(this);
  
              finishButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      **radioGroup[i].clearCheck();**
                  }
              });
              linearLayout.addView(radioGroup[i]);
          }
      }
  }
  </code></pre>
  
  <p>The error is: <code>local variable i is accessed from within inner class; needs to be declared final</code><br>
  Thanks!</p>