title: Android: Manipulating multiple (button) objects based on onClick of one object
body: <p>I'm trying to write a simple baseball scorekeeping app, mostly as a learning exercise.</p>
  
  <p>At this point I have a fully functional app. It has a single activity and a single layout. All the numbers (balls, strikes, outs, innings) are displayed as buttons. I have extended the android.widget.Button class so that the button's text is an integer, and used onClick to increment the value by 1 when the button is clicked. The object also stores a maximum value; when the increment reaches that value, the counter is reset to 0. So for example the "Balls" button carries a max value of 4 and counts 0, 1, 2, and 3. When you hit it the next time it flips from 3 back to 0. (Apologies to those who don't know or care anything about baseball.)</p>
  
  <p>All this works fine as far as it goes (source code for my extended button class shown below). Now, I'm trying to change it so that when one counter goes back to 0, the others do also. I'm at a loss as to how to do this. My first instinct was to just add in to the same 'if' statement that flips the value back to 0 like so:</p>
  
  <pre><code>final ScoreButton strikes = (ScoreButton) findViewById(R.id.strikes);
  strikes.zero();
  </code></pre>
  
  <p>(where "strikes" is a ScoreButton object defined in the activity). This comes back with a Null Pointer error. </p>
  
  <p>My second thought was to add a boolean attribute that the increment method could set when it goes back to 0 ("reset"). But I don't understand where to read this attribute. I can check it once in the onResume method, but trying to do something like a "while" loop to read the variable repeatedly just locked up the app without even displaying the main layout.</p>
  
  <p>Trying to research a better way to do this led me to reading about AsyncTask, which seems like overkill, and I'm not sure it would even work since the task (namely checking to see if a specific button has been reset) doesn't end.</p>
  
  <p>At this point it seems like this is something so simple to do that I must have missed something obvious. I'd appreciate any suggestions you have.</p>
  
  <p>Code for my custom button class:</p>
  
  <pre><code>import android.widget.Button;
  import android.content.Context;
  import android.view.View;
  import android.util.AttributeSet;
  
  public class ScoreButton extends Button  {
  
  protected int flipCount;
  protected int currCount;
  protected boolean reset;
  
  public ScoreButton(Context context) {
         super(context);
      }
  
  public ScoreButton(Context context, AttributeSet attr)  {
          super(context, attr);
          setOnClickListener(incr);
          setOnLongClickListener(dec);
  }
  
  public void init(int start, int max ) {
      flipCount = max;   /** number at which the counter goes back to 0 **/
      currCount = start; /** number to start at **/
      reset = false;
      setText(Integer.toString(currCount));
  }
  
  /** reset the button value to 0 **/
  public void zero() {
      currCount = 0;
      setText(Integer.toString(currCount));
  }
  
  
  private OnClickListener incr = new OnClickListener()  {
      public void onClick(View v) {
      currCount++; /** increment number on button **/
      if (currCount == flipCount) { /** if at the maximum value, go back to 0 **/
          currCount = 0;
          reset = true;  
          final ScoreButton strikes = (ScoreButton) findViewById(R.id.strikes);
          strikes.zero(); 
          }
      setText(Integer.toString(currCount)); /** display the new button text **/
  
      } 
  } ;
  
  /** this method decreases the value by 1 on a long click **/
  private OnLongClickListener dec = new OnLongClickListener() {
      public boolean onLongClick(View v) {
      currCount--;
      if (currCount == -1) {
          currCount=0;
      }
      setText(Integer.toString(currCount));
      return true;
  }
  } ;
  }
  </code></pre>