Having trouble creating a stopwatch timer with Calendar class

101 views Asked by At

I have been having a little trouble using the Calendar class to create a stopwatch timer. For some reason, my program keeps crashing when this piece of code is called. I tried using other classes such as Timer & Date, but the same thing happened. My code is as follows:

private OnClickListener clearButtonListener = new OnClickListener()
{
    public void onClick(View v)
    {
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        fullTime = calendar.HOUR + ":" + calendar.MINUTE + ":" + calendar.SECOND
                    + ":" + calendar.MILLISECOND;

        timeTextView.setText(fullTime);
    }
};

Should I use another class (other than the Stopwatch class) to fix this problem? Thanks a bunch.

Please do not vote down on this post. If this post is unclear in any way, please express your concern in the comments section. Thank you.

1

There are 1 answers

2
Reven On

Have you initialised the "calender" or "timeTextView"? Maybe you can post the exception first. initialise calender by call Calendar.getInstance();