-1

I need to start the countdown timer in this activity. It should be started from the button. Below I bring the code snippets.

I think I did everything right, but it doesn't work - why is this?

public class Step5 extends AppCompatActivity {


Button mgo;
public TextView timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.step5);
    timer = (TextView) findViewById(R.id.timer);
    mgo = (Button) findViewById(R.id.go);

    mgo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        new CountDownTimer(900000,1000) {
            @Override

            public void onTick(long millisUntilFinished) {
                timer.setText((int)millisUntilFinished/1000);

            }

            @Override
            public void onFinish() {
                timer.setText("Done");

            }
        }.start();
    }
});
}}

Button in xml

<Button
    android:id="@+id/go"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#FF3D00"
    android:textColor="#ffffff"
    android:text="@string/Start"
    android:layout_alignParentBottom="true"/> 
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Change this `timer.setText((int)millisUntilFinished/1000)`; to `timer.setText(""+(int)millisUntilFinished/1000);` and if it does not work yet tell us what is wrong – Raghunandan Nov 05 '17 at 16:25
  • Maybe could be a context problem. Try declare CountDownTimer as activity variable and after start on click. If you need and example, you can see example on this question: https://stackoverflow.com/questions/10032003/how-to-make-a-countdown-timer-in-android – Suaro Nov 05 '17 at 16:29
  • Tell us what you think it should do and what it is doing. – cliff2310 Nov 05 '17 at 16:36
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Henry Nov 05 '17 at 16:36
  • @Raghunandan, thank you so much!!!!!!! I did not notice this little thing. It works!!! – ded-logoped Nov 05 '17 at 16:43

2 Answers2

0

The problem is in the timer.setText((int)millisUntilFinished/1000); part.

timer.SetText() needs a string as an argument.

So try the following timer.setText(String.valueOf((int)millisUntilFinished/1000));

It will work 100 %.

0

The problem lies on the below code snippet:

timer.setText((int)millisUntilFinished/1000);

setText() method accepts only String as argument type while you are passing an int type of argument. So you could try to use the valueOf() static method of String class in order to convert your int value type into a String Object. Below is the suggested solution:

timer.setText(String.valueOf((int)millisUntilFinished/1000));

Nevertheless it should be noticed that if you compile you submitted code you will get the below compilation error:

error: incompatible types: int cannot be converted to String

This means that your code will be compiled with error and you will get noticed by the compiler for sure.

Additionally if you would use a Java IDE like Netbeans then you would get noticed for this error by a notification like below:

incompatible types: int cannot be converted to String

So concluding lets say that errors like these can be avoided by reading IDE's warnings and as a second step the compiler's messages.