如果你想要一个可以为空的浮点数,那么试试Float而不是float
Float oikonomia= new Float(vprosvasis7.getText().toString());
但它永远不会像你的例子那样是空的……
编辑:Aaaah,我开始明白你的问题了.你真的不知道vprosvasis7包含什么,以及它是否正确初始化为数字.所以试试这个,然后:
Float oikonomia = null;
try {
oikonomia = new Float(vprosvasis7.getText().toString());
}
catch (Exception ignore) {
// We're ignoring potential exceptions for the example.
// Cleaner solutions would treat NumberFormatException
// and NullPointerExceptions here
}
// This happens when we had an exception before
if (oikonomia == null) {
// [...]
}
// This happens when the above float was correctly parsed
else {
<一分快三计划稳赚would treat NumberFormatException// and NullPointerExceptions here
}
// This happens when we had an exception before
if (oikonomia == null) {
// [...]
}
// This happens when the above float was correctly parsed
else {
// [...]
}
当然,有很多方法可以简化上述内容并编写更清晰的代码.但我认为这应该解释如何正确安全地将String解析为float而不会遇到任何异常……