java.util.ArrayList 无法转换为 com.android.billingclient.api.SkuDetails

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

我正在尝试在我的应用程序中实施 Revenuecat。当我尝试购买时出现此错误:

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

在这一行中:Purchases.getSharedInstance().purchaseProduct(this, (SkuDetails) skuList, new MakePurchaseListener() {

我只想购买一个产品。谁能帮我解决这个问题?他们的 sdk 参考在 Kotlin 中。我寻求帮助他们没有帮助。

这是代码:

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

            }
        });
    }

首先,在 onReceived 中,您将 List<SkuDetails> skuList 传递给期望 List<String> 的方法 - 它应该是:

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

然后,根据您的上下文,您应该遍历 skuList 并单独购买每个项目,或者修改 Purchases.getSharedInstance().purchaseProduct 的签名以与 List<SkuDetails>

一起使用

更新:迭代 skuList 以单独购买每个 skuItem。

负面影响:消息数量 Purchase complete 与 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) {}
            });
      }
  }