Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:09:46+00:00 2026-06-01T20:09:46+00:00

I’m trying to write a simple baseball scorekeeping app, mostly as a learning exercise.

  • 0

I’m trying to write a simple baseball scorekeeping app, mostly as a learning exercise.

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.)

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:

final ScoreButton strikes = (ScoreButton) findViewById(R.id.strikes);
strikes.zero();

(where “strikes” is a ScoreButton object defined in the activity). This comes back with a Null Pointer error.

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.

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.

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.

Code for my custom button class:

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;
}
} ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:09:47+00:00Added an answer on June 1, 2026 at 8:09 pm

    You are correct to not attempt the AsynkTask method you described. That is not correct.

    Here’s my suggestion:
    You can give all the ScoreButtons a more complex onClickListener that you would define in your Activity (from your Activity’s scope, not the ScoreButton class’s scope). This would be ONE listener that contains information about all the Buttons as member variables. You would only create one instance of it but pass it to all your ScoreButtons [or Buttons through setOnClickListener(). When one of the scores needs to be wrapped, you simply set all the other counters in the Listener to 0. [Note: In this approach, you would not even need to subclass Button although you can if you want].

    However, since you said you’re learning, you might even try creating a ScoreButtonGroupView that you could place in your view hierarchy. This would be a nice self-contained solution that has internal logic for setting the buttons’ values appropriately (as I think you were trying to do by subclassing Button).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.