'while' 语句在不抛出异常的情况下无法完成 - Android

'while' statement cannot complete without throwing an exception - Android

使用这种方法,我每秒更新 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.

我讨厌警告,我想修复它。我该如何解决这个警告,或者对于这个无限循环有更好的解决方案...?

此警告是误报;你应该忽略它。

通常,无限循环是某处错误的标志;在您的代码中,无限循环正是您想要的。

如果你想要每秒触发一些东西,你可以按照这个方法,你可以确保它会在你离开时删除回调。这会每秒调用一次,直到您完成 activity。如果需要,您甚至可以在生命周期中更早地删除回调,例如在 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;
    }
};

你可能在使用 IntelliJ IDEA 时收到了这个警告,对吧?

正如其他人已经提到的,通常可以忽略此警告。

如果要禁用它,请打开设置并转到 编辑器 > 检查 并搜索 Infinite loop statement。只需取消选中该框即可完成。

一般来说,您只需将文本光标放在您希望禁用的警告上,然后按 Alt + Enter,然后单击检查信息字段,然后单击 禁用检查

您可能正在使用 Android Studio 或 IntelliJ。

如果是这样,您可以在包含无限循环的方法上方添加此代码以抑制警告:

@SuppressWarnings("InfiniteLoopStatement")

或在语句上方添加此 "magic" 注释:

//noinspection InfiniteLoopStatement

这将告诉 IDE 这是可以的。

更一般地说,当您收到误报警告时,请执行 Alt+Enter 并执行下面屏幕截图中显示的操作(select class 仅当您的 class满是误报,同样的警告)

More info here on Jetbrains help