该问题通常由以下两个原因导致:
购买商品的SKU(Stock Keeping Unit,库存保持单位)未正确设置或未与Google Play Console上的商品ID匹配。
应用程序中的代码没有正确初始化BillingClient或调用queryPurchases()方法,导致无法正确识别购买的SKU。
要解决此问题,可以按照以下步骤进行操作:
确保在Google Play Console中正确设置商品ID,并将其与应用程序中的SKU相匹配。
检查应用程序中的代码,确保已正确初始化BillingClient,并在需要的时候调用queryPurchases()方法。
下面是一个使用Kotlin编写的示例代码,用于初始化BillingClient并查询购买列表:
private lateinit var billingClient: BillingClient
fun initBillingClient() {
billingClient = BillingClient.newBuilder(this)
.setListener(this)
.enablePendingPurchases()
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
queryPurchases()
} else {
// Handle error
}
}
override fun onBillingServiceDisconnected() {
// Try to reconnect
}
})
}
fun queryPurchases() {
val purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
if (purchasesResult.responseCode == BillingClient.BillingResponseCode.OK) {
val purchases = purchasesResult.purchasesList
// Handle purchases
} else {
// Handle error
}
}
通过正确设置SKU或商品ID,并正确初始化BillingClient并调用queryPurchases()方法,您应该能够解决此问题。