2011-05-02 123 views
8

下面是代碼:爲什麼Android服務崩潰與NullPointerException?

public class BillingService extends Service implements ServiceConnection { 
... 
    @Override 
    public void onStart(Intent intent, int startId) { 
     handleCommand(intent, startId); // line 361 
    } 

    /** 
    * The {@link BillingReceiver} sends messages to this service using intents. 
    * Each intent has an action and some extra arguments specific to that action. 
    * @param intent the intent containing one of the supported actions 
    * @param startId an identifier for the invocation instance of this service 
    */ 
    public void handleCommand(Intent intent, int startId) { 
     String action = intent.getAction(); 
     if (Debug.Yes) { 
      Log.i(TAG, "handleCommand() action: " + action); 
     } 
     if (Consts.ACTION_CONFIRM_NOTIFICATION.equals(action)) { 
      String[] notifyIds = intent.getStringArrayExtra(Consts.NOTIFICATION_ID); 
      confirmNotifications(startId, notifyIds); 
     } else if (Consts.ACTION_GET_PURCHASE_INFORMATION.equals(action)) { 
      String notifyId = intent.getStringExtra(Consts.NOTIFICATION_ID); 
      getPurchaseInformation(startId, new String[] { notifyId }); 
     } else if (Consts.ACTION_PURCHASE_STATE_CHANGED.equals(action)) { 
      String signedData = intent.getStringExtra(Consts.INAPP_SIGNED_DATA); 
      String signature = intent.getStringExtra(Consts.INAPP_SIGNATURE); 
      purchaseStateChanged(startId, signedData, signature); 
     } else if (Consts.ACTION_RESPONSE_CODE.equals(action)) { 
      long requestId = intent.getLongExtra(Consts.INAPP_REQUEST_ID, -1); 
      int responseCodeIndex = intent.getIntExtra(Consts.INAPP_RESPONSE_CODE, 
        ResponseCode.RESULT_ERROR.ordinal()); 
      ResponseCode responseCode = ResponseCode.valueOf(responseCodeIndex); 
      checkResponseCode(requestId, responseCode); 
     } 
    } 

這裏是logcat的:

java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException 
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063) 
at android.app.ActivityThread.access$3600(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at tv.kinobaza.billing.BillingService.onStart(BillingService.java:361) 
at android.app.Service.onStartCommand(Service.java:420) 
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053) 
... 10 more 
+0

什麼都行361點到你的BillingService.java什麼碼? – 2011-05-02 12:02:46

回答

11

我認爲最可能的原因是因爲intentnull。根據用於onStartCommand的文檔:

intent

Intent供給到startService(Intent),給出。這可能是null如果進程已經走了之後,正在重新啓動該服務,它以前返回除了START_STICKY_COMPATIBILITY

+0

我該怎麼做才能避免這種情況? – 2011-05-02 12:43:13

+0

@Jean Hominal,如果'intent == null'應該怎麼辦? – 2011-05-02 15:21:49

+1

@LA_:您是否檢查過(例如在調試器中),意圖爲空?之後,如果您發現給定的意圖*爲空,那麼......這是您的申請。重新閱讀我引用的文檔片段 - 它解釋了在哪種情況下它可能爲空。之後,你和開發應用程序的人是唯一可以決定做什麼的人。 (例如,你可以完全避免一個空的Intent,或者你可以決定什麼也不做。) – 2011-05-02 15:40:58

4
@Override 
public void onStart(Intent intent, int startId) { 
    handleCommand(intent, startId); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    handleCommand(intent, startId); 
    return START_NOT_STICKY; 
} 
+0

如何避免對任務handleCommand(intent,startId)的雙重調用;因爲onStart()和onStartCommand都會被調用?這是一個很好的設計嗎? @Rookie – 2013-11-12 16:35:10

相關問題