Questo è il mio Service
che viene chiamato sul pulsante clic da uno Activity
. Se faccio scorrere verso sinistra il mio numero Activity
mentre il Service
è in esecuzione si blocca. Ho anche provato a eseguirlo in un processo separato inserendo android:process=":remote"
nel manifest ma è sempre lo stesso.Servizio Android con START_STICKY arresti anomali sull'app uccisione
@Override
public void onCreate(){
super.onCreate();
Log.d("Service", "Creating");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Log.d("Started", "Service");
type = intent.getIntExtra("Type",-1);
mode = intent.getIntExtra("Mode",-1);
rank = intent.getIntExtra("Rank", -1);
latitude = intent.getDoubleExtra("Lat", -1.0);
longitude = intent.getDoubleExtra("Long", -1.0);
startTime = intent.getLongExtra("Start", 0);
endTime = intent.getLongExtra("End", 0);
L'errore che sto ottenendo è:
java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.routofy.routofytest.MyLocationService.onStartCommand(MyLocationService.java:89)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2896)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Cioè null
puntatore sul type = intent.getIntExtra("Type",-1);
capisco che uccidere l'applicazione sta uccidendo il processo e Intent
questo Service
sta ottenendo è venuta fuori essere null
. Ma come gestire questi casi che, anche se il numero viene ucciso, viene consegnato a Service
? Desidero che il servizio sia totalmente indipendente da Activity
. Anche io sono in esecuzione le cose come:
Intent pickIntent = new Intent(getApplicationContext(),MyLocationService.class);
Possibile duplicato di [Che cos'è una NullPointerException e come posso risolverlo?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i- fix-it) – petey