Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointer at startIntentSenderForResult, inappbilling V3

I have the following methods (which works fine the first time):

public void TipUs(){ 
    String sku="tip_us";

    try {
    Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                                                   sku, "inapp", "TIP_US");
    PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
    startIntentSenderForResult(pendingIntent.getIntentSender(), 
                               1001, new Intent(), 
                               Integer.valueOf(0), Integer.valueOf(0),
                               Integer.valueOf(0));
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (SendIntentException e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == 1001) 
        {       
            int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString("productId");
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
}

If I try to use the same method again (what should be possible according to the settings in Google Play) I get the following error:

java.lang.NullPointerException
at com.appiclife.tipcal.Tip_Calculator.TipUs(Tip_Calculator.java:521)
at com.appiclife.tipcal.Tip_Calculator.onClick(Tip_Calculator.java:350)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

Does anyone have the error, since I used exactly the demo from Google, what should I change?

EDIT: Looks like the mService is null (though weird thing is, it worked for once, so looks almost like an issue if you're purchasing more than once the same item). I have the following:

This is my code:

OnCreate()

Intent("com.android.vending.billing.InAppBillingService.BIND"),
                    mServiceConn, Context.BIND_AUTO_CREATE);

AND:

IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {

     public void onServiceConnected(ComponentName name, 
              IBinder service) {

                System.out.println("Test!");
               mService = IInAppBillingService.Stub.asInterface(service);
     }

     public void onServiceDisconnected(ComponentName name) {
       mService = null;
     }


};

onServiceConnected is never called. I didn't see this in the manual: http://developer.android.com/google/play/billing/billing_integrate.html

but I tried to add the following service to the Manifest, without result (I removed it again, should I declare the service there?):

<service android:name="com.android.vending.billing.IInAppBillingService" />
like image 848
Diego Avatar asked Feb 10 '13 21:02

Diego


1 Answers

I ran into the same problem. You need to CONSUME the purchased item before the user can purchase the same SKU item again.

When you setup buyIntentBundle variable, you can check the RESPONSE_CODE. If the RESPONSE_CODE is 7, then the users previous purchase needs to be consumed by the app.

RESPONSE_CODE IDs can be found here: http://developer.android.com/google/play/billing/billing_reference.html#billing-codes

Query for the purchased items using this sample code: http://developer.android.com/google/play/billing/billing_integrate.html#QueryPurchases

In the INAPP_PURCHASE_DATA_LIST data is a JSON string with the "purchaseToken". Pass this token to the CONSUME function.

Once you've CONSUME the purchase, then the user can purchase it again.

like image 79
Mike C Avatar answered Nov 01 '22 05:11

Mike C