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

  • Home
  • SEARCH
  • 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 7027733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:17:14+00:00 2026-05-28T00:17:14+00:00

I’m feeling very, very stupid right now… I feel like I must be missing

  • 0

I’m feeling very, very stupid right now… I feel like I must be missing something really obvious.

I’ve encountered variations of this problem on multiple occasions now, however here is my current example –

When the activity is created, there will be a button marked Start and text set to –:– next to it. I would like to have it so that when the button is pressed, a timer starts from one minute and displays the seconds remaining in the –:– text as 00:59 etc. etc., and the text on the button changes to Pause. If the button is pressed when the timer is running, it pauses the timer and changes the text on the button to Start.

So I was using a boolean, timerRunning, in order to keep track of whether the timer was running or not. But if I try to change timerRunning within the onClickListener it gives an error and tells me to change timerRunning to final, and then once I do that it says “The final local variable timerRunning cannot be assigned, since it is defined in an enclosing type.”

I’m sorry if this is unclear – I’m just really confused with where I should be declaring variables / how to access them etc. in Android, I don’t really understand why I keep getting weird errors all the time.

Thanks.

public class Timing extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timing); 

        Button bStartJammer = (Button) findViewById(R.id.buttonStartJammer);

        CountDownTimer cdtJammerTimer; 
        long lJammerTotalMS = 60000; 
        final boolean timerRunning = false;  

        bStartJammer.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                timerRunning = 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-05-28T00:17:14+00:00Added an answer on May 28, 2026 at 12:17 am

    Without source context, it’s tough to visualize what you’re doing.

    How are you defining your click handler? If it’s an anonymous class, you’ll run into the final issues–is your activity or handler so complex that it makes a separate class completely necessary?

    In the previous question my click handler was implemented by the activity, so it has access to that instance’s variables. A much-abbreviated skeleton of what I had been doing before not using the CountDownTimer:

    public class FooTimer extends Activity implements View.OnClickListener {
        private CountDownTimer timer;
        private TextView timerDisplay;
        private Button pauseButton;
        private boolean timerRunning = false;
        private boolean timerDone = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            pauseButton = (Button) findViewById(R.id.pause_button);
            pauseButton.setOnClickListener(this);
    
            timerDisplay = (TextView) findViewById(R.id.timer_display);
            timerDisplay.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));
    
            timer = newTimer();
            timerRunning = true;
            timer.start();
        }
    
        public void onClick(View view) {
            switch (view.getId()) {
            case R.id.pause_button:
                toggleTimer();
                break;
            }
        }
    
        private void toggleTimer() {
            if (timerRunning) {
                timer.cancel();
                pauseButton.setText(getResources().getString(R.string.resume_label));
                timerRunning = false;
            } else if (timerDone) {
                finishActivity(0);
            } else {
                seconds += 1;
                timer = newTimer();
                timer.start();
                pauseButton.setText(getResources().getString(R.string.pause_label));
                timerRunning = true;
            }
        }
    
        private CountDownTimer newTimer() {
            millis = (minutes * 60 * 1000) + ((seconds + 1) * 1000);
            return new CountDownTimer(millis, 1000) {
                public void onTick(long millisUntilFinished) {
                    timerDisplay.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));
                }
    
                public void onFinish() {
                    timerDisplay.setText("Finished");
                    timerRunning = false;
                    timerDone = true;
                    pauseButton.setText(getResources().getString(R.string.back_label));
                }
            };
        }
    }
    

    (I took a bunch of stuff out, and added some really early code back in, so it’s a bit of a mish-mosh, but the ideas are there.)

    • 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
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.