这是我第一次在这里问一个问题,而且我对java和android开发都很陌生。我想创建一个具有CountDownTimer的简单应用程序,并在应用程序启动时开始倒计时。 strings.xml中有一些日期,它们在转换为日期之前也会被解析。你能告诉我为什么我的CountDownTimer在android studio虚拟设备中工作,但不能在我的实际设备上工作吗?提前谢谢。
@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();
答案 0 :(得分:0)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//write your code
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
答案 1 :(得分:0)
问题与onCreate()
方法中的try catch块有关我还创建了另一个扩展CountDownTimer
的类并使onTick中的setText在新线程上运行,现在它可以工作了。