我有2個編輯文本字段ETPredictKm(long value)和ETPredictFuelQty(double value)。如果我插入一個值,當我點擊相應的EditText時,另一個值會自動生成。我正在使用onFocusListener。我的問題是,當我插入km值來計算fuelQty時,它會得到適當的計算。但是,當我輸入燃料數量,然後單擊ETPredictKm時,我得到無效的長整數:「」異常。長無效:「」異常
請幫我提一下你的建議。 謝謝。
這裏是一些代碼:
try
{
predictKm = Long.parseLong(ETPredictKm.getText().toString()); //Get the error here
predictFuelQty = Double.parseDouble(ETPredictFuelQty.getText().toString());
}
catch(NumberFormatException ne)
{
ne.printStackTrace();
}
if(isChkLastMileage1 ==true || isChkLastMileage5==true||isChkLastMileage10==true)
{
if(ETPredictKm.hasFocus())
{
if(predictFuelQty!=0)
{
//predictionMileage is double too
predictKm =(long) (predictionMileage*predictFuelQty);
ETPredictKm.setText(String.valueOf(predictKm));
}
}
else if(ETPredictFuelQty.hasFocus())
{
// This value is calculated properly
if(predictKm!=0)
{
predictFuelQty =predictKm/predictionMileage;
ETPredictFuelQty.setText(new DecimalFormat("##.##").format(predictFuelQty)+" Litres");
}
}
}
我得到這個工作,把解析語句放入單獨的try-catch塊。不知道它是否是一種正確的做法,但爲我工作。謝謝你的解釋和你的時間,但我確信它只會擴展我的知識。 –