0

Can you tell me where is the problem on this line: timerText.setText(seconds);.

public class ShowTimer extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timer_test_xml);

        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            int seconds;
            TextView timerText = (TextView) findViewById(R.id.TimerTestId);
            @Override
            public void run() {
                seconds++;
                timerText.setText(seconds);
            }
        }, 0, 1000);

    }}
KenD
  • 5,280
  • 7
  • 48
  • 85

4 Answers4

0

I think what you want to do is display seconds in the text view. However, the TextView.setText(int) function does not do this (Im not actually sure what it does). What you want to do is timerText.setText(""+seconds); to convert the parameter into a string and change the function call to a different overloaded function.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

seconds is an int, whereas I think you want to be passing as character sequence, or a reference to one via a resource id, as per the documentation.

Richard Horrocks
  • 419
  • 3
  • 19
0

Though this doesn't answer the OP's original question, there are alternative (and - if you agree with the recommendations from the Android docs - better) ways to do this described in this thread.

Community
  • 1
  • 1
Richard Horrocks
  • 419
  • 3
  • 19
0

As with Richard's suggestion, your other problem is updating the TextView on the non-UI thread, so consider using a Handler.

Example

public class ShowTimer extends Activity {

    private Handler mHandler;
    private TextView timerText = null;
    private int seconds;

    private Runnable timerRunnable = new Runnable() {
        @Override
        public void run() {
            timerText.setText(String.valueOf(seconds++));
            mHandler.postDelayed(timerRunnable, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timer_test_xml);

        mHandler = new Handler();

        timerText = (TextView) findViewById(R.id.TimerTestId);
        timerRunnable.run();
    }
}
kheld
  • 151
  • 3