Sum in TextView

2

I have a Json that returns the positive and negative values, and set the text in my TextView . This is the calculation I try to do, but it returns an error of invalid double :

public double num1,num2,resultado;

saldo = (TextView) findViewById(R.id.saldouser);
negativo = (TextView) findViewById(R.id.vendaliv);
positivo = (TextView) findViewById(R.id.compraliv);
date = (TextView) findViewById(R.id.datatrans);

num1 = Double.parseDouble(positivo.getText().toString());
num2 = Double.parseDouble(negativo.getText().toString());
resultado = num1-num2;
System.out.println(resultado);
saldo.setText(resultado);

The error is:

  

Caused by: java.lang.NumberFormatException: Invalid double: ""

    
asked by anonymous 27.07.2017 / 16:06

1 answer

2

Of the two one:

1) You use in the text a value such as 12,9 or 10,0 com comma, as we use in Brazil. But Double uses. as it is used in the USA, so it can not interpret that 12,9 is 12.9 and explode.

2) You try to parse an empty String, which is invalid tb because it does not automatically convert to 0.

Suggestion, use a try catch block to handle this exception and / or a function to convert your comma to the point if that is the problem.

    
27.07.2017 / 16:19