Search code examples
javaandroidin-app-purchaserevenuecat

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


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) {

            }
        });
    }

Solution

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