0

I'm trying to get the values from a Edit text and trying to calcluate and put it in a textview. but im getting a error while converting to string from double.

This is my edit text code:

<EditText
     android:id="@+id/c"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:ems="10"
     android:gravity="center"
     android:inputType="number" >
    <requestFocus />
</EditText>
<EditText
            android:id="@+id/b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center"
            android:inputType="number" >

            <requestFocus />
        </EditText>
<EditText
    android:id="@+id/a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:gravity="center"
    android:inputType="number" >
    <requestFocus />
</EditText>

Here is my Activity class :

a= (EditText) findViewById(R.id.a);
b= (EditText) findViewById(R.id.b);
c= (EditText) findViewById(R.id.c);
String aa= a.getText().toString();
String bb= b.getText().toString();
String cc= c.getText().toString();
Double total= (Double.valueOf(aa)*Double.valueOf(bb)) + (Double.valueOf(aa)*Double.valueOf(cc)) ;
totalbill = Double.toString(total);
Button submitbutn = (Button) findViewById(R.id.submitbutton);
submitbutn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {       
            d.setText(totalbill);
        }
    });
}

The error log is :

10-08 07:27:58.115: E/AndroidRuntime(1909): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vat.calculator/com.vat.calculator.Home}: java.lang.NumberFormatException: Invalid double: ""
10-08 07:27:58.115: E/AndroidRuntime(1909):     at java.lang.Double.parseDouble(Double.java:295)
10-08 07:27:58.115: E/AndroidRuntime(1909):     at java.lang.Double.valueOf(Double.java:332)
10-08 07:27:58.115: E/AndroidRuntime(1909):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
10-08 07:27:58.115: E/AndroidRuntime(1909):     at java.lang.StringToReal.parseDouble(StringToReal.java:248)
4

7 回答 7

1

Try this code....

    a= (EditText) findViewById(R.id.a);
    b= (EditText) findViewById(R.id.b);
    c= (EditText) findViewById(R.id.c);

    Button submitbutn = (Button) findViewById(R.id.submitbutton);
    submitbutn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String aa= a.getText().toString();
            String bb= b.getText().toString();
            String cc= c.getText().toString();

            double aDouble = aa.length() > 0 ? Double.valueOf(aa) : 0;
            double bDouble = bb.length() > 0 ? Double.valueOf(bb) : 0;
            double cDouble = cc.length() > 0 ? Double.valueOf(cc) : 0;

            Double total= (aDouble * bDouble) + (aDouble * cDouble) ;
            totalbill = Double.toString(total);

            d.setText(totalbill);
        }
    });
于 2013-10-08T11:41:26.670 回答
0

Before Double.parse please check wether the String is not empty.

if(aa != null && aa.length() > 0){
   Double.parse(aa);
}
于 2013-10-08T11:34:13.347 回答
0

Change this line

totalbill = Double.toString(total);

to

totalbill = total.toString();

comment me about result

于 2013-10-08T11:34:21.843 回答
0

You can try with parseDouble();

Double total= (Double.parseDouble(aa)*Double.parseDouble(bb))...

And check aa!=null bb!=null and cc!=null and not empty

于 2013-10-08T11:36:47.647 回答
0

Try below code:

 if(aa != null && aa.length() > 0 && bb ! = null && bb.length>0 && cc!=null && cc.length>0){
        Double total= ((Double.parseDouble(aa))*(Double.parseDouble(bb)) + ((Double.parseDouble(aa))*(Double.parseDouble(cc)) ;
    }
于 2013-10-08T11:37:59.913 回答
0

Your application may be throwing a null pointer exception. so to avoid this try this:

if(aa != null && aa.length() > 0 && bb ! = null && bb.length>0 && cc!=null && cc.length>0){
        Double total= ((Double.parseDouble(aa))*(Double.parseDouble(bb)) + ((Double.parseDouble(aa))*(Double.parseDouble(cc)) ;
    }
于 2013-10-08T11:40:34.657 回答
0

Trim the Strings and parse to double and check the length of the string is not empty

if(aa != null && aa.trim().length() > 0){
  Double.valueOf(aa.trim())
}
于 2013-10-08T11:41:00.923 回答