java “while”语句无法在不引发异常的情况下完成 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28113856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'while' statement cannot complete without throwing an exception - Android
提问by KiKo
With this method I'm updating TextView
every second.
使用这种方法,我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();
}
}
This method is working perfect. But I got this warning:
这个方法是完美的。但我收到了这个警告:
'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.
'while' 语句不能在不抛出异常的情况下完成。
报告只能通过抛出异常退出的 for、while 或 do 语句。虽然这些陈述可能是正确的,但它们通常是编码错误的征兆。
I hate warnings and I want to fix it. How can i fix this warning, or there is a better solution for this infinite loop...?
我讨厌警告,我想修复它。我该如何解决这个警告,或者这个无限循环有更好的解决方案......?
回答by SLaks
This warning is a false positive; you should ignore it.
此警告是误报;你应该忽略它。
Usually, an infinite loop is a sign of a mistake somewhere; in your code, an infinite loop is exactly what you want.
通常,无限循环是某处错误的标志;在您的代码中,无限循环正是您想要的。
回答by Louis CAD
You are probably using Android Studio or IntelliJ.
您可能正在使用 Android Studio 或 IntelliJ。
If so, you can add this above your method containing the infinite loop to suppress warnings:
如果是这样,您可以在包含无限循环的方法上方添加此内容以抑制警告:
@SuppressWarnings("InfiniteLoopStatement")
Or add this "magic" comment above the statement:
或者在语句上方添加这个“神奇”的注释:
//noinspection InfiniteLoopStatement
This will tell the IDE that this is ok.
这将告诉 IDE 这没问题。
More generally, when you get a false positive warning, do Alt+Enter
and do what showed on the screenshot below (select class only if your class is full of false positive, for the same warning)
更一般地,当您收到误报警告时,请Alt+Enter
执行以下屏幕截图中显示的操作(仅当您的班级充满误报时才选择班级,以获得相同的警告)
回答by Jay Snayder
If you want something to fire every second you can follow this methodology where you are sure that it will remove the callback later when you leave. This calls itself every second until you are done with the activity. You could even remove the callbacks earlier if you want in the lifecycle such as within onPause() or onStop() if so desired.
如果您希望每秒触发某些东西,您可以遵循这种方法,您可以确定稍后离开时它会删除回调。这会每秒调用一次,直到您完成活动。如果需要,您甚至可以在生命周期中更早地删除回调,例如在 onPause() 或 onStop() 中(如果需要)。
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
handler.post(updateTime);
}
@Override
protected void onDestroy()
{
super.onDestroy();
handler.removeCallbacks(updateTime);
}
private final Runnable updateTime = new Runnable()
{
public void run()
{
try
{
// 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));
handler.postDelayed(this, 1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private String addZeroInFront(long diffInHours)
{
String s = "" + diffInHours;
if (s.length() == 1)
{
String temp = s;
s = "0" + temp;
}
return s;
}
};
回答by Ercksen
You probably got this warning using IntelliJ IDEA, right?
您可能使用 IntelliJ IDEA 收到此警告,对吗?
As the others already mentioned, this warning can usually be ignored.
正如其他人已经提到的,通常可以忽略此警告。
If you want to disable it, open Settings and go to Editor > Inspectionsand search for Infinite loop statement
. Simply uncheck the box and you're done.
如果要禁用它,请打开“设置”并转到“编辑器”>“检查”并搜索Infinite loop statement
. 只需取消选中该框即可完成。
And in general, you can simply place your text cursor over a warning you wish to disable and press Alt + Enter, then click on the inspection info field and click Disable inspection.
通常,您可以简单地将文本光标放在要禁用的警告上并按Alt + Enter,然后单击检查信息字段并单击禁用检查。