1

Help me please, i did my fist android app, chronometer, but something is wrong here and i dont know what. When I clicked Start the chronometer runs normal, but when I clicked Stop(or Reset), then Start again to continue it will be 00:0(, 00:0*, 00:0/,...

My Code:

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

/**
 * Created by Admin on 6/26/2015.
 */
public class Tab1 extends Fragment implements View.OnClickListener {
    private Button buttonStart;
    private Button buttonStop;
    private Button buttonReset;
    private Chronometer chronometerRun;
    long time = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.tab_1,container,false);

        buttonStart=(Button)v.findViewById(R.id.buttonStrart);
        buttonStop=(Button)v.findViewById(R.id.buttonStop);
        buttonReset=(Button)v.findViewById(R.id.buttonReset);
        chronometerRun=(Chronometer)v.findViewById(R.id.chronometerRunning);

        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
        buttonReset.setOnClickListener(this);

        return v;
    }

    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.buttonStrart:
                chronometerRun.setBase(SystemClock.elapsedRealtime() + time);
                chronometerRun.start();
                break;
            case  R.id.buttonStop:
                time = chronometerRun.getBase()+SystemClock.elapsedRealtime();
                chronometerRun.stop();
                break;
            case R.id.buttonReset:
                chronometerRun.setBase(SystemClock.elapsedRealtime());
                chronometerRun.stop();
                break;

        }


    }
}

1 Answer 1

0

What I understand from your code is that you are using setBase method in a wrong way. It allows you to set the base time which chronometer automatically takes as reference. Then the Chronometer automatically updates its View by differentiating the current time with the value set in setBase method i.e. Chronometer sets the current time to SystemClock.elapsedRealtime() - getBase().

Since you are adding time to SystemClock.elapsedRealtime(), it gives negative values for the current time when your time is non-zero.

Instead what you should do is to set time to SystemClock.elapsedRealtime()-chronometerRun.getBase() at stop in order to keep how much time has passed since the beginning of the chronometer and then subtract time from SystemClock.elapsedRealtime() while setting the base to keep the last time difference at pause and you should set time to 0 at reset so that to start the chronometer from zero i.e. setting the base time to the current time.

The below switch case code works fine :

switch(v.getId())
    {
        case R.id.buttonStart:
            Log.i("test",""+SystemClock.elapsedRealtime() + time);
            chronometerRun.setBase(SystemClock.elapsedRealtime()-time) ;
            chronometerRun.start();
            break;
        case  R.id.buttonStop:
            Log.i("test",""+SystemClock.elapsedRealtime() + time);
            time = SystemClock.elapsedRealtime()-chronometerRun.getBase();
            chronometerRun.stop();
            break;
        case R.id.buttonReset:
            Log.i("test",""+SystemClock.elapsedRealtime() + time);
            time=0;
            chronometerRun.setBase(SystemClock.elapsedRealtime());
            chronometerRun.stop();
            break;

    }

Note: It is always useful to use Log.i("TAG",""+variableToWatch) to watch the relevant variables while debugging to see how they are changing.

I added Log.i("test",""+SystemClock.elapsedRealtime() + time); right after all of your case statements to see the value of time

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.