应用内结算-快速设备定向-导致崩溃(IllegalStateException) [英] In App Billing - Rapid Device Orientation - causes crash (IllegalStateException)

查看:98
本文介绍了应用内结算-快速设备定向-导致崩溃(IllegalStateException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据Android的实现应用内广告实现了应用内广告(v3)结算指南.

I implemented In-app Billing (v3) according to Android's Implementing In-app Billing guide.

一切正常,直到我旋转设备,然后立即将其旋转回其原始方向.实际上,有时它可以正常工作,有时它会因以下原因而崩溃:

All works fine, until I rotate the device, then immediately rotate it back to it's original orientation. Actually, sometimes it works, and sometimes it crashes with:

java.lang.IllegalStateException: IabHelper was disposed of, so it cannot be used.

尽管我不是很肯定,但这似乎与IAB的异步特性有关.

Seems this is related to the asynchronous nature of IAB, though I'm not positive.

有什么想法吗?

推荐答案

您可能会遇到异常,因为在活动生命周期的某个地方调用了mHelper.dispose(),然后稍后尝试使用相同的已处置实例.我的建议是只在onDestroy()中处置mHelper,然后在onCreate()中重新创建它.

You're probably getting the exception because somewhere in the activity lifecycle, you called mHelper.dispose(), then tried to use that same disposed instance later on. My recommendation is to only dispose of mHelper in onDestroy() and recreate it in onCreate().

但是,您会遇到IabHelper和设备旋转的另一个问题.问题是这样的:在活动的onCreate()中,创建IabHelper实例mHelper并进行设置.稍后,您调用mHelper.launchPurchaseFlow(...),并且IAB弹出对话框将浮动在您的活动上方.然后旋转设备,并在onDestroy(...)中处置IabHelper实例,然后在onCreate(...)中重新创建. IAB对话框仍然显示,您按购买按钮,购买完成.然后在您的活动中调用onActivityResult(),您自然会调用mHelper.handleActivityResult(...).问题是,从未在重新创建的IabHelper实例上调用launchPurchaseFlow(...).如果先前已在当前实例上调用launchPurchaseFlow(...),则IabHelper仅在handleActivityResult(...)中处理活动结果.您的OnIabPurchaseFinishedListener将永远不会被调用.

However, you will run into another problem with IabHelper and device rotation. The problem goes like this: in your activity's onCreate(), you create the IabHelper instance mHelper and set it up. Later, you call mHelper.launchPurchaseFlow(...) and the IAB popup dialog appears floating above your activity. Then you rotate the device, and the IabHelper instance gets disposed of in onDestroy(...) then recreated in onCreate(...). The IAB dialog is still showing, you press the purchase button, and the purchase completes. onActivityResult() is then called on your activity, and you naturally call mHelper.handleActivityResult(...). The problem is, launchPurchaseFlow(...) has never been called on the recreated instance of IabHelper. IabHelper only handles the activity result in handleActivityResult(...) if launchPurchaseFlow(...) has been previously called on the current instance. Your OnIabPurchaseFinishedListener will never be called.

我对此的解决方案是修改IabHelper,使您可以告诉它期望handleActivityResult(...)而无需调用launchPurchaseFlow(...).我添加了以下内容 IabHelper.java

My solution to this was to modify IabHelper to allow you to tell it to expect handleActivityResult(...) without calling launchPurchaseFlow(...). I added the following to IabHelper.java

public void expectPurchaseFinished(int requestCode, OnIabPurchaseFinishedListener listener)
{
    mRequestCode = requestCode;
    mPurchaseListener = listener;
}

这将导致IabHelper在调用handleActivityResult(...)时在侦听器上调用onIabPurchaseFinished(...).然后,您执行以下操作:

This will cause IabHelper to call onIabPurchaseFinished(...) on the listener when handleActivityResult(...) is called. Then, you do this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    mHelper.expectPurchaseFinished(requestCode, mPurchaseFinishedListener);
    mHelper.handleActivityResult(requestCode, resultCode, data);
}

我的IabHelper的整个副本可以在这里找到 https://gist.github.com/benhirashima/7917645 .请注意,我已使用此提交,它修复了一些错误,并且尚未在Android SDK Manager中发布.另外请注意,有较新的提交,但它们包含

My entire copy of IabHelper can be found here https://gist.github.com/benhirashima/7917645. Note that I updated my copy of IabHelper with the version found in this commit, which fixes a few bugs and has not been published in the Android SDK Manager. Also note that there are newer commits, but they contain new bugs and should not be used.

这篇关于应用内结算-快速设备定向-导致崩溃(IllegalStateException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆