通过这种方法,我每秒更新一次 TextView
。
private void UpdatingTime(final String endTime, final long diffInDays) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(ONE_SECOND);
mHandler.post(new Runnable() {
@Override
public void run() {
// Updating time every second
long diffInHours = Methodes.diffInHours(endTime, diffInDays);
long diffInMinutes = Methodes.diffInMinutes(endTime, diffInDays, diffInHours);
long diffInSeconds = Methodes.diffInSeconds(endTime, diffInDays, diffInHours, diffInMinutes);
tvTime2.setText(addZeroInFront(diffInHours)
+ ":" + addZeroInFront(diffInMinutes)
+ ":" + addZeroInFront(diffInSeconds));
}
private String addZeroInFront(long diffInHours) {
String s = "" + diffInHours;
if (s.length() == 1) {
String temp = s;
s = "0" + temp;
}
return s;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
}
这个方法效果很好。但是我收到了这个警告:
'while' statement cannot complete without throwing an exception.
Reports for, while, or do statements which can only exit by throwing an exception. While such statements may be correct, they are often a symptom of coding errors.
我讨厌警告,我想修复它。我该如何解决这个警告,或者对于这个无限循环有更好的解决方案...?
最佳答案
此警告是误报;你应该忽略它。
通常,无限循环是某处错误的标志;在您的代码中,无限循环正是您想要的。
关于java - 'while' 语句无法在不抛出异常的情况下完成 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113856/