null 不能强制转换为非 null 类型 kotlin.collections.List<com.android.billingclient.api.Purchase>



我的大多数应用程序用户都没有遇到任何崩溃。然而,我的一些应用程序用户遇到了这个致命的异常:

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

请告诉我如何解决这个问题,谢谢。这个问题出现了同样的错误,但显然还没有找到修复方法。

如前所述,错误指向强制铸造result.getPurchasesList()

当您的应用程序处于这种状态时,您需要检查这种情况,并获取更多关于它返回的内容和原因的数据。

至于上面的答案和Unresolved reference: it上的错误,使用takeIf必须传递谓词,因此必须将这些括号更改为{}

billingClient.takeIf{it.isReady}?.let {

检查https://medium.com/mobile-app-development-publication/using-kotlin-takeif-or-takeunless-c9eeb7099c22

Kotlin比许多级联if…更具表现力

此外,Kollin在编译时处理null,所以IDE应该通知您是否为null。

我试图重写你的代码,因为它是不可破解的。老实说,我不知道它是否有效,是否做了同样的事情(它应该这样做,但正如我所说,如果很难理解的话(,但我发现它更可读,更可调试。

记住,为了删除所有if=null可以使用?。操作员

希望它能帮助你理解Kotlin的表现力

private fun loadAllSKUs() {
billingClient.takeIf{it.isReady}
?.let {
val params = SkuDetailsParams.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build()
billingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
skuDetailsList
?.takeIf{ billingResult.responseCode === BillingClient.BillingResponseCode.OK && skuDetailsList.isNotEmpty() }
?.filter {it.sku.equals("product")}
?.forEach { skuDetails -> 
val result: Purchase.PurchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
val purchases: List<Purchase>? = result.getPurchasesList() as? List<Purchase>
val isOwned = purchases?.any { it.sku == "product" }?:false

if(!isOwned){
val billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(skuDetails)
.build()
billingClient.launchBillingFlow(skuDetails@ActivityClass, billingFlowParams)
}
}
}
}

相关内容

最新更新