title: ImageView Button Toggle in Android
body: <p>I'm trying to make an ImageView button toggle when I click on it. I've got the below code:</p>
  
  <pre><code>    ImageView button01 = (ImageView) findViewById(R.id.button01);
      button01.setOnClickListener(new OnClickListener() {
          int button01pos = 0;
          public void onClick(View v) {
              if (button01pos == 0) {
                  button01.setImageResource(R.drawable.image01);
                  button01pos = 1;
              } else if (button01pos == 1) {
                  button01.setImageResource(R.drawable.image02);
                  button01pos = 0;
              }
          }
      });
  </code></pre>
  
  <p>But for some reason button01 is underlined in red in Eclipse and it gives the error:</p>
  
  <blockquote>
    <p>Cannot refer to a non-final variable button01 inside an inner class
    defined in a different method</p>
  </blockquote>
  
  <p>Does anyone know why it's doing this and how to fix it?</p>
  
  <p>Thanks</p>