我收到以下错误(在logcat中),当我尝试在我的设备上启动时从EditText中删除数字0.0(双数据类型值)时,我的应用程序在我的Android设备上崩溃..
以下是错误消息:
以下是代码:
/*
* variables prefixed by 'v' are variables of primitive data type
* variables prefixed by 'et' are edit text
* variables prefixed by 'cl' are changeListeners of watcher data type
*
*/
public class MhrSolankiTipCalc extends Activity {
private static final String BILL_BEFORE_TIP = "BILL_BEFORE_TIP";
private static final String CURRENT_TIP = "CURRENT_TIP";
private static final String FINAL_BILL = "FINAL_AMOUNT";
double vBillAmount, vTipAmount, vFinalBillAmount;
EditText etBillAmount, etTipAmount, etFinalBillAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mhr_solanki_tip_calc);
if (savedInstanceState == null) {
vBillAmount = 0.0;
vTipAmount = 0.0;
vFinalBillAmount = 0.0;
} else {
vBillAmount = savedInstanceState.getDouble(BILL_BEFORE_TIP);
vTipAmount = savedInstanceState.getDouble(CURRENT_TIP);
vFinalBillAmount = savedInstanceState.getDouble(FINAL_BILL);
}
etBillAmount = (EditText) findViewById(R.id.billBlankEditView);
etTipAmount = (EditText) findViewById(R.id.tipBlankEditView);
etFinalBillAmount = (EditText) findViewById(R.id.finalEditView);
etBillAmount.addTextChangedListener(clBillAmount);
etTipAmount.addTextChangedListener(clTipAmount);
}
private TextWatcher clBillAmount = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
try {
vBillAmount = Double.parseDouble(s.toString());
} catch (NumberFormatException e) {
vBillAmount = 0.0;
}
catch(NullPointerException e)
{
vBillAmount=0.0;
}
updateBillAndFinalAmount();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
private TextWatcher clTipAmount = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
vTipAmount = Double.parseDouble(s.toString());
}
catch(NumberFormatException e){
vTipAmount = 0.0;
}
updateBillAndFinalAmount();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
public void updateBillAndFinalAmount() {
double vTipAmount = Double
.parseDouble(etTipAmount.getText().toString());
double vBillAmount = Double.parseDouble(etBillAmount.getText()
.toString());
double vFinalBillAmount = vBillAmount + (vBillAmount * vTipAmount);
etFinalBillAmount.setText(String.format("%.02f", vFinalBillAmount));
}
答案 0 :(得分:1)
在updateBillAndFinalAmount()
中,您使用EditTexts的值为空字符串调用Double.parseDouble
。您应该像在文本观察者中那样检查它们
double vTipAmount;
double vBillAmount;
try {
vTipAmount = Double.parseDouble(etTipAmount.getText().toString());
} catch (NumberFormatException e) {
vTipAmount = 0.0;
}
try {
vBillAmount = Double.parseDouble(etBillAmount.getText().toString());
} catch (NumberFormatException e) {
vBillAmount = 0.0;
}
而不是复制/粘贴整个块,你可以简单地编写一个按特定规则返回双精度的函数
public static double getDoubleFromString(String s)
{
double d;
try {
d = Double.parseDouble(s);
} catch (NumberFormatException e) {
d = 0.0;
}
return d;
}
,您的代码看起来像
double vTipAmount = getDoubleFromString(etTipAmount.getText().toString());
double vBillAmount = getDoubleFromString(etBillAmount.getText().toString());
答案 1 :(得分:1)
在提供从EditText
到parseDouble()
的字符串值之前,请确保它不为空:
vTipAmount = (s.toString().length > 0 && s.toString() != null)
? Double.parseDouble(s.toString())
: 0.0;
另一种方法是捕获所有异常而不仅仅是NumberFormatException(虽然它增加了一点点开销):
try {
vBillAmount = Double.parseDouble(s.toString());
} catch (Exception e) {
vBillAmount = 0.0;
}
答案 2 :(得分:1)
由于您已经在TextWatchers中解析vBillAmount
和vTipAmount
以及捕获NumberFormatException
,因此您无需在updateBillAndFinalAmount()
方法中再次执行此操作。这也将修复您在此方法中获得的NumberFormatException
。将其更改为:
public void updateBillAndFinalAmount()
{
double vFinalBillAmount = vBillAmount + (vBillAmount * vTipAmount);
etFinalBillAmount.setText(String.format("%.02f", vFinalBillAmount));
}