简体   繁体   中英

I am trying to create a Stopwatch app and am having problems adding a lap button and continue button that work

As the title suggests I am trying to create a Stopwatch app and am having problems adding a lap button and continue button that work. I have added the buttons to the layout.xml file but am having trouble with the java code that gets them to function.

I need to display the "stopped/paused" time when the lap button is clicked, the internal clock needs to keep running however and no longer be displayed, or the stopped/paused time needs to be shown in a new display whichever is easier to accomplish. When the lap timer is clicked subsequently it should display the current time (internal clock)

I need the continue button to then resume the paused/stopped time.

Could somebody help me with the code, I'm relatively inexperienced in java, so any help would be appreciated

here's the code of my main java file:

    package com.hfad.stopwatch;
    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.os.SystemClock;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Chronometer;
    import android.widget.TextView;
    public class StopwatchActivity extends Activity implements  View.OnClickListener {


Button Continue;
Button lapChrono;
Chronometer chrono;
long time =0;



//Number of seconds displayed on the stopwatch.
private int seconds = 0;
//Is the stopwatch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stopwatch);
    Continue=(Button)findViewById(R.id.Continue_button);
    lapChrono=(Button)findViewById(R.id.lap_button);
    lapChrono.setOnClickListener(this);
    Continue.setOnClickListener(this);
    chrono=(Chronometer)findViewById(R.id.chronometer);



    if (savedInstanceState != null) {
        seconds = savedInstanceState.getInt("seconds");
        running = savedInstanceState.getBoolean("running");
        wasRunning = savedInstanceState.getBoolean("wasRunning");
    }


    runTimer();
}

@Override
protected void onPause() {
    super.onPause();
    wasRunning = running;
    running = false;
}
@Override
protected void onResume() {
    super.onResume();
    if (wasRunning) {
        running = true;
    }
}


public void onClick(View arg0) {
    switch (arg0.getId()){

        case R.id.Continue_button:
            chrono.setBase(SystemClock.elapsedRealtime()+time);
            chrono.setBase(time);
            break;
        case R.id.lap_button:
            time = chrono.getBase()+SystemClock.elapsedRealtime();
            break;

    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("seconds", seconds);
    savedInstanceState.putBoolean("running", running);
    savedInstanceState.putBoolean("wasRunning", wasRunning);
}
//Start the stopwatch running when the Start button is clicked.
public void onClickStart(View view) {
    running = true;
}
//Stop the stopwatch running when the Stop button is clicked.
public void onClickStop(View view) {
    running = false;
}
//Reset the stopwatch when the Reset button is clicked.
public void onClickReset(View view) {
    running = false;
    seconds = 0;
}
//Sets the number of seconds on the timer.
private void runTimer() {
    final TextView timeView = (TextView)findViewById(R.id.time_view);
    final Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            int hours = seconds/3600;
            int minutes = (seconds%3600)/60;
            int secs = seconds%60;
            String time = String.format("%d:%02d:%02d",
                    hours, minutes, secs);
            timeView.setText(time);
            if (running) {
                seconds++;
            }
            handler.postDelayed(this, 1000);
        }
    });
}

}

I am just having problems getting things to run smoothly as I'm not too fluent in my Java, apologies, that is why help is needed. The logic isn't quite there. The continue button doesn't function properly nor does the lap, I'm needing it to function as described above.

chrono object is not initialized and used in onClick(). Hence NPE occurs. Try to initialise it in onCreate().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM