Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 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 Reply

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...