-1

This a code of timer that save information in pause and reset. every time I start app this information are put to timer. because I don't want start timer from 0. I want start timer from last time. so in the beginning It should retrieve the last time information.

public class TimerActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SharedPreferences prefs = this.getSharedPreferences("timer", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();



    timeSwapBuff=prefs.getLong("timeSwapBuff", 0);
    startTime=prefs.getLong("startTime", 0);
    updatedTime=prefs.getLong("updatedTime", 0);



    Log.i("timeSwapBuff", ""+timeSwapBuff);
    Log.i("startTime", ""+startTime);
    Log.i("updatedTime", ""+updatedTime);


        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;



    updatedTime = timeSwapBuff + timeInMilliseconds;

    int secs = (int) (updatedTime / 1000);
    int mins = secs / 60;
    secs = secs % 60;
    int milliseconds = (int) (updatedTime % 1000);


    // error at this line for set text  
    timerValue.setText("" + mins + ":"
            + String.format("%02d", secs) + ":"
            + String.format("%03d", milliseconds));




    timerValue = (TextView) findViewById(R.id.timerValue);

    startButton = (Button) findViewById(R.id.start);

    resetButton = (Button) findViewById(R.id.reset);

    pauseButton = (Button) findViewById(R.id.pause);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });


    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

            editor.putLong("timeSwapBuff", timeSwapBuff);
            editor.putLong("startTime", startTime);
            editor.putLong("updatedTime", updatedTime);


        }
    });


    resetButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {


            startTime=SystemClock.uptimeMillis();
            timeSwapBuff=0;
            customHandler.removeCallbacks(updateTimerThread);

            timerValue.setText("" + 0 + ":"
                    + String.format("%02d", 00) + ":"
                    + String.format("%03d", 000));

            editor.putLong("timeSwapBuff", timeSwapBuff);
            editor.putLong("startTime", startTime);
            editor.putLong("updatedTime", updatedTime);

        }
    });

}

private Runnable updateTimerThread = new Runnable() {

    public void run() {


        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;



        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};





}
sara sara
  • 47
  • 9

4 Answers4

0

you are setting the text to timeValue TextView before initialising it.
Change from

timerValue.setText("" + mins + ":"
            + String.format("%02d", secs) + ":"
            + String.format("%03d", milliseconds));
timerValue = (TextView) findViewById(R.id.timerValue);

to

timerValue = (TextView) findViewById(R.id.timerValue);

timerValue.setText("" + mins + ":"
            + String.format("%02d", secs) + ":"
            + String.format("%03d", milliseconds));
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
0

Swap these two around, you are calling setText on an object which is not initialized.

timerValue.setText("" + mins + ":"
        + String.format("%02d", secs) + ":"
        + String.format("%03d", milliseconds));
timerValue = (TextView) findViewById(R.id.timerValue);

becomes

timerValue = (TextView) findViewById(R.id.timerValue);
timerValue.setText("" + mins + ":"
        + String.format("%02d", secs) + ":"
        + String.format("%03d", milliseconds));
RobVoisey
  • 1,083
  • 15
  • 24
0
TextView timerValue = (TextView) findViewById(R.id.timerValue);// initialize
String value="" + mins + ":"
            + String.format("%02d", secs) + ":"
            + String.format("%03d", milliseconds);
if(value!=null) //Null Check
timerValue.setText(value);
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

You should initialise your textview before setting value to it like:

timerValue = (TextView) findViewById(R.id.timerValue);
timerValue.setText("" + mins + ":"
            + String.format("%02d", secs) + ":"
            + String.format("%03d", milliseconds));
Aakash
  • 5,181
  • 5
  • 20
  • 37