문제

I have a function in Android wherein it gets the value returned by a TextView which are totalcost_venue, totalcost_apparel, totalcost_souvenir, and totalcost_caterer.

The value is converted to double and after which I have to add those values. But I encountered an error when a particular TextView does not return any value. Any help will do. Thanks!

double totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
double totalcost_apparel_finalconverted = Double.parseDouble(totalcost_apparel.getText().toString());
double totalcost_caterer_finalconverted = Double.parseDouble(totalcost_caterer.getText().toString());
double totalcost_souvenir_finalconverted =  Double.parseDouble(totalcost_souvenir.getText().toString());

double total = (totalcost_venue_finalconverted + totalcost_apparel_finalconverted + totalcost_caterer_finalconverted +totalcost_souvenir_finalconverted);

String total_all= Double.toString(total);

totalcost_all.setText(total_all);
String totalcost_final = totalcost_all.getText().toString();

double budget_converted = Double.parseDouble(budget_event);
double totalcost_converted = Double.parseDouble(totalcost_final);

if(budget_converted <= totalcost_converted){
                registerErrorMsg.setText(" Oops!! Already exceeded");
도움이 되었습니까?

해결책

Double.parseDouble() may be throwing an exception if it recieves a null value, or it is recieves text.

To solve your issue with a null value, you could add the following check:

double totalcost_venue_finalconverted = 0;

if(!totalcost_venue.getText().toString().equals("")){
    totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top