java break 不能在循环或开关之外使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15933374/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:19:53  来源:igfitidea点击:

break cannot be used outside of a loop or a switch

java

提问by Kevin

String value = custom.getdob().toString();
if (value == null || value.equals("")) {
    holder.item3.setText("-");
    break;
}

I am actually over-riding the method getView, so i am not able to place a returnafter my if condition as it says view must be returned, but i want to exit the if condition after the check. so tried out breakand i am getting this error "break cannot be used outside of a loop or a switch"

我实际上覆盖了 getView 方法,因此我无法return在 if 条件之后放置 a ,因为它说必须返回视图,但我想在检查后退出 if 条件。所以试过了break,我收到这个错误“ break不能在循环或开关之外使用

    break; = > Throws break cannot be used outside of a loop or a switch
    return; = > This method must return a result of type View

回答by Ankit

why do you even need a breakthere? its not actually required. since anyway the ifblock has finished, control will exit the block.

为什么你甚至需要break那里?它实际上并不需要。因为无论如何if块已经完成,控制将退出块。

if you wanna return the view instead of break;, put return holder;(assuming holder is a view)

如果你想返回视图而不是break;,放return holder;(假设持有人是一个视图)

break can only be used in switch & loops (for/while/do-while)

break 只能用于 switch & loops (for/while/do-while)

回答by Pawe? Piecyk

I think that in this particular case you can return null to exit from method.

我认为在这种特殊情况下,您可以返回 null 以退出方法。

回答by Subhrajyoti Majumder

return viewObject;you must use. Seems to be method signature is

return viewObject;你必须使用。似乎是方法签名是

public View getView(){...}

公共视图 getView(){...}

And it clearly says it should return View object or null. So you can use return null;or return the expected view object.

它清楚地表明它应该返回 View 对象或 null。因此您可以使用return null;或返回预期的视图对象。