Android 使用Google Play计费库的非法状态异常
我在使用Google Play计费库时遇到非法状态异常:com.android.billingclient:Billing:1.1 这种异常只是在某些特定情况下偶尔发生。比如:每安装500次,我们就有4个错误。我们无法在本地复制此问题Android 使用Google Play计费库的非法状态异常,android,kotlin,play-billing-library,Android,Kotlin,Play Billing Library,我在使用Google Play计费库时遇到非法状态异常:com.android.billingclient:Billing:1.1 这种异常只是在某些特定情况下偶尔发生。比如:每安装500次,我们就有4个错误。我们无法在本地复制此问题 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layou
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
billingClient = BillingClient.newBuilder(this@MainActivity).setListener(this).build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
var purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
var purchaseList = purchasesResult.purchasesList
purchaseList.forEach { // LINE 89 -> MainActivity.kt
if(it.sku.equals(ExtraNames.REMOVE_ADS_SKU)){
Utils.setIntSharedPreferences(this@MainActivity, ExtraNames.REMOVE_ADS, ExtraNames.YES)
}
}
}
}
override fun onBillingServiceDisconnected() {
//Nothing to do now
}
})
...
}
这是stacktrace:
java.lang.IllegalStateException:
at com.myapp.br.MainActivity$onCreate$1.onBillingSetupFinished (MainActivity.kt:89)
at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection.onServiceConnected (BillingClientImpl.java:903)
at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:1818)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:1847)
at android.os.Handler.handleCallback (Handler.java:808)
at android.os.Handler.dispatchMessage (Handler.java:101)
at android.os.Looper.loop (Looper.java:166)
at android.app.ActivityThread.main (ActivityThread.java:7425)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
任何线索,导致此异常的原因是什么?一种可能性是,在调用
onBillingSetupFinished
时,您的活动已不再处于活动状态……请确保您正在活动onDestroy()中调用endConnection
一种可能性是,在调用onBillingSetupFinished
时,您的活动不再处于活动状态……请确保您正在活动onDestroy()中调用endConnection
我试试这个。过几天我会回复是否有效。您好,应用您的提示降低了错误率,但仍然会发生。目前,我已经添加了一个try{…}catch(e:IllegalStateException{}。停止运行时错误。顺便说一下,谢谢你的帮助。你好,卡洛斯。很抱歉,我要重提一个非常古老的话题,但我有一个非常相似的情况。queryPurchases()可能在1/100的安装中出现此错误。你有没有追根究底?我可以用try/catch来概括,但这感觉像是在治疗症状而不是原因……我会试试这个。过几天我会回复是否有效。您好,应用您的提示降低了错误率,但仍然会发生。目前,我已经添加了一个try{…}catch(e:IllegalStateException{}。停止运行时错误。顺便说一下,谢谢你的帮助。你好,卡洛斯。很抱歉,我要重提一个非常古老的话题,但我有一个非常相似的情况。queryPurchases()可能在1/100的安装中出现此错误。你有没有追根究底?我可以用try/catch来概括它,但这感觉就像是在治疗症状而不是原因。。。
override fun onDestroy() {
billingClient.endConnection()
}