Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我全部删除该值时,ContextChanged_Java_Android_Textwatcher - Fatal编程技术网

Java 当我全部删除该值时,ContextChanged

Java 当我全部删除该值时,ContextChanged,java,android,textwatcher,Java,Android,Textwatcher,我正试图建立一个小费计算。 这是我代码的一部分: public void onTextChanged(CharSequence s, int start, int before, int count) { double billAmount = Double.parseDouble(s.toString()); tip10EditText.setText("" + billAmount * .1); tip15EditText.setText("" + billAmou

我正试图建立一个小费计算。 这是我代码的一部分:

public void onTextChanged(CharSequence s, int start, int before, int count) {

    double billAmount = Double.parseDouble(s.toString());
    tip10EditText.setText("" + billAmount * .1);
    tip15EditText.setText("" + billAmount * .15);
    tip20EditText.setText("" + billAmount * .2);
    total10EditText.setText("" + (billAmount + billAmount * 0.1));
    total15EditText.setText("" + (billAmount + billAmount * 0.15));
    total20EditText.setText("" + (billAmount + billAmount * 0.2));

}
问题是,当我删除账单编辑文本中的所有内容时,应用程序崩溃。。 我的朋友找到了解决办法。这是他的密码:

public void onTextChanged(CharSequence s, int start, int before, int count) {

    if (!s.toString().equals("")) {
        bill = Double.parseDouble(s.toString());

        tip10EditText.setText((bill / 10) + "");
        total10EditText.setText((bill + (bill / 10)) + "");

        // the String.format("%.2f", is to SHORTEN the tail of the double to 2 digits
        tip15EditText.setText(String.format("%.2f", (bill / 6.6666) ));
        total15EditText.setText(String.format("%.2f",(bill + (bill / 6.6666)) ));
        tip20EditText.setText(String.format("%.2f", (bill / 5) ));
        total20EditText.setText(String.format("%.2f",(bill + (bill / 5)) ));

        // to handle the seekBar changing according to the bill
        double billCu = Double.parseDouble(billEditText.getText().toString());
        // total20EditText.setText(String.format("%.2f",(tipOf ));
        double progressPer=Double.parseDouble(customTipTextView.getText().toString());
        double tipOf = (billCu/100)*progressPer;
        tipCustomEditText.setText(String.format("%.2f",(tipOf )));
        totalCustomEditText.setText(String.format("%.2f",((billCu+tipOf) )));
    }
    else {
        tip10EditText.setText( "");
        total10EditText.setText( "");
        tip15EditText.setText( "");
        total15EditText.setText( "");
        tip20EditText.setText( "");
        total20EditText.setText( "");
        tipCustomEditText.setText("");
        totalCustomEditText.setText("");            
    }
}
有没有更好的方法来处理这个问题?

试试这个:

     if (!TextUtils.isEmpty(s)) {

     Then do your stuff....

}

将代码放入以下if语句中:

   if (s.length() > 0)

在catch块中使用try catch块和NumberFormatException,通过使用它,u也可以处理错误类型的输入

        try{
            //your code
        }catch(NumberFormatException npe){
            //handle the exception and intimate the user if needed.
        }catch(Exception e){

        }

只有当文本框中的任何输入显示为0时,上述代码才会计算正确的值。

如果输入文本字符,您会怎么做?除了检查它是否为空(使用
s.length>0
TextUtils.isEmpty
),您可能会发现
TextUtils.isDigitsOnly
很有用,或者使用
try catch
block来处理无效的输入。@Evgency Golding如果您在投票或接受ans时找到有用的答案,观众将有助于理解解决方案。
public void onTextChanged(CharSequence s, int start, int before, int count) {
   double billAmount = 0.0;
   if(s.length() != 0){
        billAmount = Double.parseDouble(s.toString());
   }
        tip10EditText.setText("" + billAmount * .1);
        tip15EditText.setText("" + billAmount * .15);
        tip20EditText.setText("" + billAmount * .2);
        total10EditText.setText("" + (billAmount + billAmount * 0.1));
        total15EditText.setText("" + (billAmount + billAmount * 0.15));
        total20EditText.setText("" + (billAmount + billAmount * 0.2));
}