Android 将十进制后3digits的精度应用于字符串获取运行时异常

Android 将十进制后3digits的精度应用于字符串获取运行时异常,android,floating-point,runtimeexception,Android,Floating Point,Runtimeexception,我将十进制后的3digits精度应用于字符串,该字符串保存一个浮点值,并在访问另一个edittext时将其设置为edittext final float f = Float.valueOf(eproductprice.getText().toString()); ebuilding.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v,

我将十进制后的3digits精度应用于字符串,该字符串保存一个浮点值,并在访问另一个edittext时将其设置为edittext

final float f = Float.valueOf(eproductprice.getText().toString());
ebuilding.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if(!hasFocus)
        {
            //Log.e("Float","Message"+f);
            String productprice=String.format("%.3f", f);
            eproductprice.setText(productprice);
        }
    }
});
在这里,我想设置格式浮点输入,ebuilding是下一个
edittext

我得到了这个错误:

java.lang.RuntimeException: Unable to start activity    
ComponentInfo{com.omega.waselapp/com.omega.waselapp.Pickupdetails_activity}:  
java.lang.NumberFormatException: Invalid float: ""

使用
eproductprice.getText()。
内部
onFocusChange
从EditText获取值:

@Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
                if(!hasFocus)
                {       
                   // get value here from EditText
                 float f = Float.valueOf(eproductprice.getText().toString()); 
                 String productprice=String.format("%.3f", f);
                 eproductprice.setText(productprice);
         }
    }
在调用
String.format
之前,请确保字符串不正确,以避免
NumberFormatException
或使用
try catch处理
NumberFormatException