Search code examples
androidin-app-billing

In app Billing crash


I was integrating In app billing into my application. I used the code below which I got from google's development site:

    buttonDownload = (Button) findViewById(R.id.download);
    buttonDownload.setOnClickListener(new OnClickListener() {

        // @Override  
          //  public void onClick(View v) {  
            // Intent i = new Intent(Intent.ACTION_VIEW);
            // i.setData(Uri.parse(url2));
            // startActivity(i);
            // link.open();
        @Override
        public void onClick(View v) {
            ArrayList skuList = new ArrayList();
            skuList.add(inappid);
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                skuDetails = mservice.getSkuDetails(3, getPackageName(),
                        "inapp", querySkus);

                int response = skuDetails.getInt("RESPONSE_CODE");
                if (response == 0) {

                    ArrayList<String> responseList = skuDetails
                            .getStringArrayList("DETAILS_LIST");

                    for (String thisResponse : responseList) {
                        JSONObject object = new JSONObject(thisResponse);
                        String sku = object.getString("productId");
                        String price = object.getString("price");
                        if (sku.equals(inappid)) {
                            System.out.println("price " + price);
                            Bundle buyIntentBundle = mservice
                                    .getBuyIntent(3, getPackageName(), sku,
                                            "inapp",
                                            "bGoa+V7g/WqDXvKRqq+JTFn4uQRbPiQJEipf9RzJ");
                            PendingIntent pendingIntent = buyIntentBundle
                                    .getParcelable("BUY_INTENT");
                            startIntentSenderForResult(
                                    pendingIntent.getIntentSender(), 1001,
                                    new Intent(), Integer.valueOf(0),
                                    Integer.valueOf(0), Integer.valueOf(0));
                        }
                    }
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SendIntentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

I used the android.test.purchased to test the in app billing. When I click on the download button it buys the test opbject without flaws. But as soon as I try to buy it again the app crashes, the item people can buy in my app must be buyable multiple times. Can someone tell me how to fix this?


Solution

  • The problem had nothing to do with a NullPointerException, Google In app billing V3 won't let you buy a object if you already have one in your inventory, that's why you need to consume it first. You do this by adding something like this to your code:

      mservice.consumePurchase(3, getPackageName(), purchaseToken);