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.
Instead of passing
int secondsin constructor,You should pass it through
Intentand inonCreate()of theActivityand then you can start and schedule the timer for thisActivity.onCreate()may be considered as Constructor for theActivity.