0

I am wonder why this code doesn't work. Is it because of the constructor?

import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.content.Intent;
import android.app.Activity;
import java.util.Timer;
import java.util.TimerTask;

public class ClockMain extends Activity {

int Hours;
int Minutes;
int Seconds;

String TimeOfDayS;

TextView HoursMainV;
TextView MinutesMainV;
TextView SecondsMainV;
TextView TimeOfDayMainV;

Timer oneSecond;

public ClockMain(int Seconds) {
    // TODO Auto-generated constructor stub
    oneSecond = new Timer();
    oneSecond.schedule(new SecondsTask(), Seconds * 1000);

}
class SecondsTask extends TimerTask {
    public void run() {
        new ClockMain(1);
        ++Seconds;
        if(Seconds == 60){
            ++Minutes;
            Seconds = 0;
            if(Minutes == 60) {
                ++Hours;
                Minutes = 0;
                if(Hours == 12){
                    if(TimeOfDayS.equals("AM")) {
                        TimeOfDayS = "PM";
                    } else{
                        TimeOfDayS = "AM";
                    }
                    Hours= 0;
                }
            }
        }
        HoursMainV = (TextView) findViewById(R.id.HoursMainV);
        HoursMainV.setText(""+Hours);

        MinutesMainV = (TextView) findViewById(R.id.MinutesMainV);
        MinutesMainV.setText(":"+Minutes);

        SecondsMainV = (TextView) findViewById(R.id.SecondsMainV);
        SecondsMainV.setText(":"+Seconds);

        TimeOfDayMainV = (TextView) findViewById(R.id.TimeOfDayMainV);
        TimeOfDayMainV.setText(" "+TimeOfDayS);
    }
}
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.clock_main);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
    int Hour = extras.getInt("HoursS");
    Hour = Hours;
    int Minute = extras.getInt("MinutesS");
    Minute = Minutes;
    int Second = extras.getInt("SecondsS");
    Second = Seconds;
    String TimeOfDaySs = extras.getString("TimeOfDayS");
    TimeOfDaySs = TimeOfDayS;
    HoursMainV = (TextView) findViewById(R.id.HoursMainV);
    HoursMainV.setText(""+Hour);

    MinutesMainV = (TextView) findViewById(R.id.MinutesMainV);
    MinutesMainV.setText(":"+Minute);

    SecondsMainV = (TextView) findViewById(R.id.SecondsMainV);
    SecondsMainV.setText(":"+Second);

    TimeOfDayMainV = (TextView) findViewById(R.id.TimeOfDayMainV);
    TimeOfDayMainV.setText(" "+TimeOfDaySs);
    new ClockMain(1);

}
  }

And yes I did change the Manifest and I know there is not a problem with the xml. It stopped running when I added the public Clock Main and the SecondsTask Class. Please help. If there is a better way of doing this specific to android, I would love to learn it or if I am missing something I would like to know that too.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
Pillager225
  • 439
  • 1
  • 3
  • 15

2 Answers2

3

Instead of passing int seconds in constructor, You should pass it through Intent and in onCreate() of the Activity and then you can start and schedule the timer for this Activity.

onCreate() may be considered as Constructor for the Activity.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • 1
    [For few more details](http://stackoverflow.com/questions/3302177/android-activity-constructor-vs-oncreate) – Adil Soomro Oct 21 '11 at 05:54
  • Alright. I am a little new so can you provide an example of what you are talking about please? Thank you very much. – Pillager225 Oct 21 '11 at 15:27
0

I think, the problem is with the views that are being updated in your SecondsTask run() method. TimerTask runs in a separate thread. In android, only the main thread can update the UI. You are updating the UI from a worker thread. So remove that UI updation task from this run method. To update the UI, use Handlers or runOnUiThread methods... I hope you got the asnswer....

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127