为什么Android的服务崩溃,并抛出NullPointerException异常?抛出、异常、Android、NullPointerException

2023-09-04 23:40:15 作者:热情太够、

下面是code:

 公共类BillingService有扩展服务实现ServiceConnection {
...
    @覆盖
    公共无效ONSTART(意向意图,诠释startId){
        handleCommand(意向,startId); //线361
    }

    / **
     *在{@link BillingReceiver}将消息发送到意向使用这项服务。
     *每个意图有一个动作和一些额外的参数具体行动。
     *参数意图包含支持的行动之一的意图
     *参数startId标识符为这个服务的调用实例
     * /
    公共无效handleCommand(意向意图,诠释startId){
        串动= intent.getAction();
        如果(Debug.Yes){
            Log.i(TAG,handleCommand()动作:+动作);
        }
        如果(Consts.ACTION_CONFIRM_NOTIFICATION.equals(动作)){
            的String [] notifyIds = intent.getStringArrayExtra(Consts.NOTIFICATION_ID);
            confirmNotifications(startId,notifyIds);
        }否则如果(Consts.ACTION_GET_PURCHASE_INFORMATION.equals(动作)){
            字符串notifyId = intent.getStringExtra(Consts.NOTIFICATION_ID);
            getPurchaseInformation(startId,新的String [] {notifyId});
        }否则如果(Consts.ACTION_PUR​​CHASE_STATE_CHANGED.equals(动作)){
            字符串的signedData = intent.getStringExtra(Consts.INAPP_SIGNED_DATA);
            特征码= intent.getStringExtra(Consts.INAPP_SIGNATURE);
            purchaseStateChanged(startId,签名数据,签名);
        }否则,如果(Consts.ACTION_RESPONSE_ code.equals(动作)){
            长的requestId = intent.getLongExtra(Consts.INAPP_REQUEST_ID,-1);
            INT响应codeINDEX = intent.getIntExtra(Consts.INAPP_RESPONSE_ code,
                    响应code.RESULT_ERROR.ordinal());
            响应code响应code =响应code.valueOf(响应codeINDEX);
            checkResponse code(的requestId,响应code);
        }
    }
 

下面是LogCat中:

 了java.lang.RuntimeException:无法启动服务tv.kinobaza.billing.BillingService@45129a30与空:显示java.lang.NullPointerException
在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
在android.app.ActivityThread.access $ 3600(ActivityThread.java:125)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2096)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4627)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:876)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
在dalvik.system.NativeStart.main(本机方法)
显示java.lang.NullPointerException:产生的原因
在tv.kinobaza.billing.BillingService.onStart(BillingService.java:361)
在android.app.Service.onStartCommand(Service.java:420)
在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
... 10更多
 
2016手机故障率排行榜,看看有没有你的手机

解决方案

我觉得最有可能的原因是因为意图。根据该文件onStartCommand:

  

意图

     

意图供给 startService(意向),是既定的。这可能是如果该服务被重新启动后它的进程已经走了,它已经previously除了 START_STICKY_COMPATIBILITY

Here is the code:

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);
        }
    }

Here is the LogCat:

java.lang.RuntimeException: Unable to start service tv.kinobaza.billing.BillingService@45129a30 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

解决方案

I believe the most likely cause would be because intent would be null. According to the documentation for onStartCommand:

intent

The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY