This is my first time asking a question on here, and I'm quite new to java and android development. I wanted to create a simple app that has a CountDownTimer and starts a countdown as the app is launched. There are certain dates in strings.xml and they're also parsed before being converted into dates. Could you please tell me why my CountDownTimer works in the android studio virtual device, but not on my actual device? Thanks in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
TextView helloTextView = findViewById(R.id.helloTextView); //set the textView
final TextView countDownTextView = findViewById(R.id.countdownTextView); //set the textView
TextView untilTextView = findViewById(R.id.untilTextView); // set the textView
Random rand = new Random();
int n4 = rand.nextInt(3);
final TypedArray bgImg = getResources().obtainTypedArray(R.array.bgImg);
int backgroundID = bgImg.getResourceId(n4, 3);
final ImageView background = findViewById(R.id.bgImgView);
background.setImageResource(backgroundID);
Button complimentBtn = findViewById(R.id.complimentBtn);
complimentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, Compliment.class));
}
});
Button funnyBtn = findViewById(R.id.funnyBtn);
funnyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, Pop.class));
}
});
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
final int n1 = rand.nextInt(8);
Date rightNow = Calendar.getInstance().getTime();
final Date firstDate;
final Date secondDate;
final Date thirdDate;
final Date endOfFirstDate;
final Date endOfSecondDate;
final String[] words = getResources().getStringArray(R.array.words);
final String[] dates = getResources().getStringArray(R.array.dates);
helloTextView.setText("Hello " + words[n1] + "!");
untilTextView.setText("until then);
try {
firstDate = formatter.parse(dates[0]);
secondDate = formatter.parse(dates[1]);
thirdDate = formatter.parse(dates[2]);
endOfFirstDate = formatter.parse(dates[3]);
endOfSecondDate = formatter.parse(dates[4]);
} catch (ParseException e) {
e.printStackTrace();
return;
}
Date untilNextDate = firstDate;
if (rightNow.before(firstDate)){
untilNextDate = firstDate;
}
else if (rightNow.after(firstDate) && rightNow.before(endOfFirstDate))
{
untilNextDate = endOfFirstDate;
}else if (rightNow.after(endOfFirstDate) && rightNow.before(secondDate)) {
untilNextDate = secondDate;
} else if (rightNow.after(secondDate) && rightNow.before(endOfSecondDate)) {
untilNextDate = endOfSecondDate;
} else if (rightNow.after(endOfSecondDate) && rightNow.before(thirdDate)) {
untilNextDate = thirdDate;
}
long rightNowMilli = rightNow.getTime();
long untilNextDateMilli = untilNextDate.getTime();
long millisUntilFinished = untilNextDateMilli - rightNowMilli;
final Date temp = untilNextDate;
final long secondsInMilli = 1000;
final long minutesInMilli = secondsInMilli * 60;
final long hoursInMilli = minutesInMilli * 60;
final long daysInMilli = hoursInMilli * 24;
final CountDownTimer cdt = new CountDownTimer(millisUntilFinished, 1000) {
@Override
public void onTick(long millisUntilFinished) {
final long elapsedDays = millisUntilFinished / daysInMilli;
millisUntilFinished = millisUntilFinished % daysInMilli;
final long elapsedHours = millisUntilFinished / hoursInMilli;
millisUntilFinished = millisUntilFinished % hoursInMilli;
final long elapsedMinutes = millisUntilFinished / minutesInMilli;
millisUntilFinished = millisUntilFinished % minutesInMilli;
final long elapsedSeconds = millisUntilFinished / secondsInMilli;
countDownTextView.setText(elapsedDays + " days " + elapsedHours + " hours " + elapsedMinutes + " minutes " + elapsedSeconds + " seconds");
}
@Override
public void onFinish() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
};cdt.start();
The problem had something to do with the try catch block inside the onCreate()
method I also created another class that extends CountDownTimer
and made the setText inside onTick run on a new thread, now it works.