2013-07-16 98 views
1

我從Activity開始Service,然後我將Activity綁定到它。 如果我嘗試強制關閉任務管理器中的應用程序,將調用Service onStartCommand()方法,並調用NullPointerException。我不明白爲什麼onStartCommand()被調用。Android服務onStartCommand()在應用程序部隊關閉時觸發

該服務剛onStartCommand()重寫現在:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Bundle bundle = intent.getExtras(); //This is line 24 
    //some stuff to get strings from bundle 
    return super.onStartCommand(intent, flags, startId); 
} 

例外:

07-16 14:49:02.190: E/AndroidRuntime(24607): FATAL EXCEPTION: main 
07-16 14:49:02.190: E/AndroidRuntime(24607): java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.app.ActivityThread.access$2000(ActivityThread.java:152) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1385) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.os.Looper.loop(Looper.java:137) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.app.ActivityThread.main(ActivityThread.java:5328) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at java.lang.reflect.Method.invokeNative(Native Method) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at java.lang.reflect.Method.invoke(Method.java:511) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at dalvik.system.NativeStart.main(Native Method) 
07-16 14:49:02.190: E/AndroidRuntime(24607): Caused by: java.lang.NullPointerException 
07-16 14:49:02.190: E/AndroidRuntime(24607): at com.app.GameService.onStartCommand(GameService.java:24) 
07-16 14:49:02.190: E/AndroidRuntime(24607): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2765) 
07-16 14:49:02.190: E/AndroidRuntime(24607): ... 10 more 
+0

你有你的活動/服務的onDestroy()的任何代碼? –

+0

不,絕對。 – Emanuele

回答

0

你可以使用某事像這樣

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Bundle bundle = getIntent.getExtras(); 
    if (bundle!=null) { 
     Bundle bundle = intent.getExtras(); 
     } 
    } else { 
     Log.i("Log", "Bundle is null"); 

    } 
} 
+0

'getIntent()'沒有爲服務定義。我解決了檢查'intent!= null',但我不明白爲什麼onStartCommand()被調用。 – Emanuele

相關問題