我的大多数应用程序的用户都没有遇到任何崩溃。然而,我的一些应用程序用户得到了这个致命的异常。
Fatal Exception: java.lang.NullPointerException: null cannot be cast to non-null type kotlin.collections.List<com.android.billingclient.api.Purchase>
at com.company.appname.ActivityClass$loadAllSKUs$1.onSkuDetailsResponse(ActivityClass.java:10)
at com.android.billingclient.api.zzj.run(zzj.java:7)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7178)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
这是我的代码。我用它来查看一个用户是否已经购买了应用内购买。如果他没有,就启动计费流程。
private lateinit var billingClient: BillingClient
private val skuList = listOf("product")
private boolean isOwned = false
private fun loadAllSKUs() {
if (billingClient.isReady)
val params = SkuDetailsParams.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build()
billingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
if (skuDetailsList != null)
if (billingResult.responseCode === BillingClient.BillingResponseCode.OK && !skuDetailsList.isEmpty())
for (skuDetailsObject in skuDetailsList)
val skuDetails = skuDetailsObject
if (skuDetails.sku.equals("product"))
val result: Purchase.PurchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
val purchases: List<Purchase> = result.getPurchasesList() as List<Purchase>
for (purchase in purchases)
val thisSKU = purchase.sku
if (thisSKU == "product")
isOwned = true;
// System.out.println("OWNED")
if(isOwned == false){
val billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(skuDetails)
.build()
billingClient.launchBillingFlow(this@ActivityClass, billingFlowParams)
请告诉我如何解决这个问题,谢谢。This question得到同样的错误,但显然还没有找到修复方法。