inAppBilling v3中的NullPointerException

时间:2014-01-23 04:38:24

标签: java android in-app-billing

01-23 05:32:26.747: E/AndroidRuntime(14589): java.lang.NullPointerException
01-23 05:32:26.747: E/AndroidRuntime(14589):    at com.azeandroid.sudokufree.util.IabHelper.launchPurchaseFlow(IabHelper.java:401)
01-23 05:32:26.747: E/AndroidRuntime(14589):    at com.azeandroid.sudokufree.util.IabHelper.launchPurchaseFlow(IabHelper.java:338)

我在代码

之后的论坛中看到了“return”的相同问题
if (listener != null) listener.onIabPurchaseFinished(r, null);

但我的代码中有“return”语句,但仍然给出相同的错误。

错误代码:

act.startIntentSenderForResult(pendingIntent.getIntentSender(),
                                       requestCode, new Intent(),
                                       Integer.valueOf(0), Integer.valueOf(0),
                                       Integer.valueOf(0));

完整的launchPurchaseFlow方法:

public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
                    OnIabPurchaseFinishedListener listener, String extraData) {
    checkNotDisposed();
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    if (itemType.equals(ITEM_TYPE_SUBS) && !mSubscriptionsSupported) {
        IabResult r = new IabResult(IABHELPER_SUBSCRIPTIONS_NOT_AVAILABLE,
                "Subscriptions are not available.");
        flagEndAsync();
        if (listener != null) listener.onIabPurchaseFinished(r, null);
        return;
    }

    try {
        logDebug("Constructing buy intent for " + sku + ", item type: " + itemType);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));
            flagEndAsync();
            result = new IabResult(response, "Unable to buy item");
            if (listener != null) listener.onIabPurchaseFinished(result, null);
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        mPurchasingItemType = itemType;
        act.startIntentSenderForResult(pendingIntent.getIntentSender(),
                                       requestCode, new Intent(),
                                       Integer.valueOf(0), Integer.valueOf(0),
                                       Integer.valueOf(0));
    }
    catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();
        flagEndAsync();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null) listener.onIabPurchaseFinished(result, null);
    }
    catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();
        flagEndAsync();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null) listener.onIabPurchaseFinished(result, null);
    }
}

2 个答案:

答案 0 :(得分:0)

检查pendingIntent是否为空:

PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
if (pendingIntent == null)
    Log.E(TAG, "pendingIntent is null");

答案 1 :(得分:0)

同时检查getIntentSender()是否为null。像:

if (pendingIntent.getINtentSender()!=null) 
act.startIntentSenderForResult(pendingIntent.getIntentSender(),
                                   requestCode, new Intent(),
                                   Integer.valueOf(0), Integer.valueOf(0),
                                   Integer.valueOf(0));