TextView不会返回任何Android

时间:2014-02-19 04:59:23

标签: parsing null textview double gettext

我在Android中有一个函数,它获取TextView返回的值 totalcost_venuetotalcost_appareltotalcost_souvenirtotalcost_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");

1 个答案:

答案 0 :(得分:0)

Double.parseDouble()如果收到空值,或者收到文本,可能会抛出异常。

要使用空值解决您的问题,您可以添加以下检查:

double totalcost_venue_finalconverted = 0;

if(!totalcost_venue.getText().toString().equals("")){
    totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
}