I convert the integer entered by the user to hour/minutes/seconds as follows
int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000;
i.putExtra("tiempo",time);
startService(i);
And as you can see, that number sends it to a service and executes it, the problem is that if I use it inside a service Asynctask
it throws me the following error in the logcat:
java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:533)
Inside my asyn I have it as follows:
@Override
protected void onPostExecute(String result) {
pdLoading.dismiss();
if(result.equalsIgnoreCase(""))
{
int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000; // esta es la linea del problema
i.putExtra("tiempo",time);
startService(i);
Toast.makeText(getApplicationContext(),"Comienza la cuenta.",Toast.LENGTH_SHORT).show();
The curious thing is that if I take this function out of the it async
works perfectly.
I solved the problem in the following way:
It happened that I did not receive the number of the
editText
therefore I received the exception . it was a simple mistake, but he had missed the basics.If you get this error:
indicates that it cannot cast the value "" to an integer type. In this case you can check the length to know if it
EditText
has content,However the above may still display an error message if the user enters a non-numeric value.
The best way to validate that the content of
EditText
is a value to be converted to an integerInteger.parseInt()
is to validate that the value is numeric.For this you can use the following method:
and use it like this: