应用程序因空值“而崩溃”;“双”字;数据类型(android)

应用程序因空值“而崩溃”;“双”字;数据类型(android),android,Android,当我在我的设备上启动时,试图从EditText中删除数字0.0(双数据类型值)时,我在我的android设备上出现以下错误(在logcat中),并且我的应用程序崩溃 以下是错误消息: 代码如下: /* * variables prefixed by 'v' are variables of primitive data type * variables prefixed by 'et' are edit text * variables prefixed by 'cl'

当我在我的设备上启动时,试图从EditText中删除数字0.0(双数据类型值)时,我在我的android设备上出现以下错误(在logcat中),并且我的应用程序崩溃

以下是错误消息:

代码如下:

  /*
   * 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));
}

updateBillAndFinalAmount()
中,使用EditText的值调用
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;
    }
不需要复制/粘贴整个块,只需编写一个函数,根据特定规则返回double

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());

在将从
EditText
获取的字符串值提供给
parseDouble()
之前,请确保该值不为空:

vTipAmount = (s.toString().length > 0 && s.toString() != null)
             ? Double.parseDouble(s.toString())
             : 0.0;
另一种方法是捕获所有异常,而不仅仅是NumberFormatException(不过它增加了一点最小的开销):


由于您已经在分析
vBillAmount
vTipAmount
并在TextWatchers中捕获
NumberFormatException
s,因此无需在
updateBillAndFinalAmount()
方法中再次执行此操作。这还将修复此方法中出现的
NumberFormatException
。将其更改为:

public void updateBillAndFinalAmount()
{
    double vFinalBillAmount = vBillAmount + (vBillAmount * vTipAmount);
    etFinalBillAmount.setText(String.format("%.02f", vFinalBillAmount));
}

谢谢你的回答。。。完全忘记了ifelse速记员。。虽然这样做的一件事,我宁愿考虑一个函数,如MiHIL以上回答..嗯,这是非常愚蠢的我检查变量的值,而不是在编辑文本视图中输入的字符串:P…刚刚开始安卓系统的开发,我的脑子里就没有了。。荣誉与感谢:D
public void updateBillAndFinalAmount()
{
    double vFinalBillAmount = vBillAmount + (vBillAmount * vTipAmount);
    etFinalBillAmount.setText(String.format("%.02f", vFinalBillAmount));
}