Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android countdown timer error

This is the code given by Google to create a countdown timer.

new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

    public void onFinish() {
        mTextField.setText("done!");
    }
}.start(); 

However when I place it in "MyActivity.java" I am given an error. I assume I must import "CountDownTimer" but am unsure how. P.s. I am using Android Studio.

1 Answer

Hello @TomCoomer , I went ahead and made a mock up class sort of. Below I gave a suggestion on how to fix your example and then after that I implemented a solution that I would have used had I been using this code in my project.

Try creating an inner class that extends the CountDownTimer class. Also I don't know if this is what did it but you need to override the onFinish and onTick methods. I don't know if you just pasted that code in a class and tried to run it but that was just a snippet but you will need to declare a CountDownTimer variable first and then initialize it with the constructor you used. Like CountDownTimer timer = new CountDownTimer( 30000, 10000 ){ public void onTick( )...........................etc etc }.start( );

Here is how I did it:

//Import the CountDownTimer class
import android.os.CountDownTimer;

MyActivity
{

    protected EditText mTextField;

    //Declare a member variable of my class derived from the CountDownTimer super class
    protected CustomTimer myTimer;

    @Override
    protected void onCreate( Bundle bundle )
    {

        super.onCreate( bundle );
        setContentView( R.layout.my_activity_layout );
        mTextField = ( EditText ) findViewById( R.id.my_activity_layout_text_field );

       //Initialize myTimer with its' overloaded constructor
       myTimer = new CustomTimer( 30000, 1000 );

        myCustomTimer.start( );

    } //end of onCreate method

    //Declare a new inner child class of parent class CountDownTimer
    public class CustomTimer extends CountDownTimer
    {

        //Overloaded constructor
        public CustomTimer(long millisInFuture, long countDownInterval) 
        {

            super( millisInFuture, countDownInterval );

        }

        //override onTick method
        @Override
        public void onTick(long millisUntilFinished) 
        {

            mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

        }

        //override onFinish method
        @Override
        public void onFinish() 
        {

            mTextField.setText("done!");

        }

    } //end of CustomTimer class

} //end of MyActivity class