Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

numberformatexception - Invalid Double. Number format exception in android

I have this in xml.

<EditText
                android:id="@+id/creditlimitfield2"
                android:hint="0"
                android:inputType="numberDecimal" />

So, the default value for edittext is 0.

in code, I have this

creditlimit = (EditText) findViewById(R.id.creditlimitfield2);
double credit_limit = Double.valueOf(creditlimit.getText().toString());

Alternately, I have tried:

double credit_limit = Double.parseDouble(creditlimit.getText().toString());

And

double credit_limit = new Double(creditlimit.getText().toString());

When I run the program, It gives the error of

Numberformatexception. Invalid double "".

Please be reminded that the field is not empty. Any suggestions would be helpful.


I have seen many questions,almost similar to it, with very less answers and none of them help solving my condition. So, Please do not tag it as "Similar Question" without checking other questions too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

getText() does not return the value inside android:hint. So you are trying to convert an empty string as double. use android:text property instead.

<EditText
    android:id="@+id/creditlimitfield2"
    android:text="0"
    android:inputType="numberDecimal" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...