Eilidh
Eilidh

Reputation: 1374

I'm a little confused about how to work with variables correctly in Android

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; 
            }
        });


    }
}

Upvotes: 1

Views: 220

Answers (3)

Dave Newton
Dave Newton

Reputation: 160170

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

Upvotes: 2

mr.j05hua
mr.j05hua

Reputation: 170

its not starting the code anywhere, your just setting it to true that its a timer. watch the tutorials here and they should really help you out. cornboyzandroid Some of his earlier videos really describe how to do a timer pretty clearly and step by step. And he helps with global and local variables. around episode 6 or 7 just check his page out.

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80330

Your new OnClickListener(){..} is actually an anonymous class: http://mindprod.com/jgloss/anonymousclasses.html

Anonymous classes have access to class (static) and instance fields of enclosing class. So a solution is to have timerRunning defined as a field, i.e. define it outside of onCreate() method.

Upvotes: 2

Related Questions