0

I am trying to implement Revenuecat to my app . When i tried to purchase i am getting this error :

java.util.ArrayList cannot be cast to com.android.billingclient.api.SkuDetails

In this Line : Purchases.getSharedInstance().purchaseProduct(this, (SkuDetails) skuList, new MakePurchaseListener() {

I am trying to buy only a product . Can anyone help me to solve this ? Their sdk reference is in Kotlin . I asked for help they are not helping .

Here is the code :

Show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // relativeLayout.setVisibility(View.VISIBLE);
            List<String> skuList = new ArrayList<>();
            skuList.add(ITEM_S);


            Purchases.getSharedInstance().getNonSubscriptionSkus(skuList, new GetSkusResponseListener() {
                @Override
                public void onReceived(@NonNull List<SkuDetails> skus) {
                    makepurchase(skus);


                }

                @Override
                public void onError(@NonNull PurchasesError error) {

                }
            });






        }

    });




private void makepurchase(List<SkuDetails> skuList){

        Purchases.getSharedInstance().purchaseProduct(this, (SkuDetails) skuList, new MakePurchaseListener() {
            @Override
            public void onCompleted(@NonNull Purchase purchase, @NonNull PurchaserInfo purchaserInfo) {


                Toast.makeText(UserInformation.this,"Purchase complete",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(@NonNull PurchasesError error, boolean userCancelled) {

            }
        });
    }
vifol47386
  • 17
  • 6

1 Answers1

2

First, in onReceived you're passing List<SkuDetails> skuList to the method expecting List<String> - it should be:

private void makepurchase(List<SkuDetails> skuList) {
}

Then, depending on your context you should iterate over the skuList and purchase each item separately, or modify signature of Purchases.getSharedInstance().purchaseProduct to work with List<SkuDetails>

Update: iterating the skuList to purchase each skuItem separately.

Negative side effect: as many messages Purchase complete as the size of the skuList

  private void makepurchase(List<SkuDetails> skuList) {
    for (SkuDetails skuItem : skuList) {
      Purchases.getSharedInstance()
        .purchaseProduct(this, skuItem, new MakePurchaseListener() {
              @Override
              public void onCompleted(
                  @NonNull Purchase purchase, @NonNull PurchaserInfo purchaserInfo) {

                Toast.makeText(UserInformation.this, "Purchase complete", Toast.LENGTH_LONG).show();
              }

              @Override
              public void onError(@NonNull PurchasesError error, boolean userCancelled) {}
            });
      }
  }

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Check my question i have added the line where i am getting error. – vifol47386 Apr 13 '20 at 15:08
  • Updated the answer with the details how to iterate over `skuList`. If it's not that convenient, you'll need to modify `Purchases.getSharedInstance().purchaseProduct` to accept the list of `SkuDetails` products - now it is accepting only one product. – Nowhere Man Apr 14 '20 at 18:09