我在Android中有一个函数,它获取TextView
返回的值
totalcost_venue
,totalcost_apparel
,totalcost_souvenir
和totalcost_caterer
。
该值转换为double,然后我必须添加这些值。但是当特定TextView
没有返回任何值时,我遇到了错误。任何帮助都可以。谢谢!
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");
答案 0 :(得分:0)
Double.parseDouble()
如果收到空值,或者收到文本,可能会抛出异常。
要使用空值解决您的问题,您可以添加以下检查:
double totalcost_venue_finalconverted = 0;
if(!totalcost_venue.getText().toString().equals("")){
totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
}