Problem when passing a long data to a service on Android

4

I try to pass a variable of type long to a service which is the time in which it is going to run but it marks me the following error

9-27 00:33:16.364 14153-15553/com.android.controlmovil E/AndroidRuntime: FATAL EXCEPTION: IntentService[enviar_coord_background]
  Process: com.android.controlmovil, PID: 14153
  java.lang.NumberFormatException: Invalid long: "null"
      at java.lang.Long.invalidLong(Long.java:124)
      at java.lang.Long.parseLong(Long.java:345)
      at java.lang.Long.parseLong(Long.java:321)
      at com.android.controlmovil.enviar_coord_background.onHandleIntent(enviar_coord_background.java:70)
      at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:155)
      at android.os.HandlerThread.run(HandlerThread.java:61)

My code is

Intent intent1 = new Intent(enrolar_ws.this, enviar_coord_background.class);
intent1.putExtra("rango",rango_spinner);                                    
startService(intent1);  

And in my service is

@Override
    protected void onHandleIntent(Intent intent) {
        boolean band=false;
        long rango;

        try {
           rango = Long.parseLong(intent.getStringExtra("rango"));

            while(band==true){
                try {
                    Thread.sleep(rango);
                    comenzarLocalizacion(enviar_coord_background.this);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }catch (NullPointerException e){
            Toast.makeText(getApplicationContext(),"Error"+e,Toast.LENGTH_LONG).show();
        }
        //}
    }
    
asked by Carlos Hernández 27.09.2016 в 07:27
source

2 answers

3

The problem specified in your LogCat is:

  

java.lang.NumberFormatException: Invalid long: "null"

Since within your method onHandleIntent() tries to perform a parsing to convert a value to long, but this value is null.

rango = Long.parseLong(intent.getStringExtra("rango"));

You could quickly perform a validation to avoid this problem:

if(intent.getStringExtra("rango")!= null){
rango = Long.parseLong(intent.getStringExtra("rango"));
}else{
rango = 0;
}

But it is important to review because the value you send is null from the intent, you could perform a validation through a ternary operation:

Intent intent1 = new Intent(enrolar_ws.this, enviar_coord_background.class);
intent1.putExtra("rango", rango_spinner!=null? rango_spinner:0 );                                    
startService(intent1);  
    
answered by 27.09.2016 / 14:52
source
0

The problem is that when you try the long extra rango_spinner , its value is null .

// rango_spinner = null
intent1.putExtra("rango",rango_spinner);                                   

You should do a check, check if the value is null to not start the service and show a message to the user.

For example:

if (rango_spinner != null) {
    Intent intent1 = new Intent(enrolar_ws.this, enviar_coord_background.class);
    intent1.putExtra("rango",rango_spinner);                                    
    startService(intent1);  
} else {
    Toast.makeText(getApplicationContext(),"Debes introducir un rango",Toast.LENGTH_LONG).show();  
}

So far the first problem, but ...

  • Because if we send value null there is a NumberFormatException instead of a NullPointerException ?

What happens next is that you retrieve the extra as a String , since you use intent.getStringExtra("rango") , so you do not recover null as long , but as String , so you try to parse the string "null" with what causes the:

  

NumberFormatException : Invalid long: "null"

To avoid this, insert in this way:

intent1.putExtra("rango", (long) rango_spinner);

I've done a simple casting, but maybe you should convert it with Long.valueOf or similar .

And retrieve the extra with Intent::getLongExtra(java.lang.String, long ) , with which, in addition, you will have a default value in case you do not want to make the first proposed check and you can receive erroneous data.

try {
    rango = intent.getLongExtra("rango", 2L));

    while(band==true){
        try {
            Thread.sleep(rango);
            comenzarLocalizacion(enviar_coord_background.this);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}catch (NullPointerException e){
    Toast.makeText(getApplicationContext(),"Error"+e,Toast.LENGTH_LONG).show();
}

EXTRA : in case you want to know what's going on, here's the implementation of getLongExtra :

public long getLongExtra(String name, long defaultValue) {
   return mExtras == null ? defaultValue : mExtras.getLong(name, defaultValue);
}
    
answered by 27.09.2016 в 13:50