java - IabHelper(类)Dispose(方法)未处理的异常

标签 java android

我正在尝试在我最新的 Android 项目中实现应用内购买。 为此,我遵循此 guide 。 一切都很顺利,直到我使用 dispose 方法来关闭与 Play 商店的任何通信。 我得到的是以下错误:

Error:(101, 45) error: unreported exception IabAsyncInProgressException; must be caught or declared to be thrown

在以下代码段上:

@Override
    public void onDestroy() {
        super.onDestroy();
        //Always unbind the with the store connection, otherwise performance degradation of the device may follow.
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }

深入研究 IabHelper 类 (Java) 后,我发现了 dispose 方法。 这里是该方法的代码:

    /**
     * Dispose of object, releasing resources. It's very important to call this
     * method when you are done with this object. It will release any resources
     * used by it such as service connections. Naturally, once the object is
     * disposed of, it can't be used again.
     */
    public void dispose() throws IabAsyncInProgressException {
        synchronized (mAsyncInProgressLock) {
            if (mAsyncInProgress) {
                throw new IabAsyncInProgressException("Can't dispose because an async operation " +
                    "(" + mAsyncOperation + ") is in progress.");
            }
        }
        logDebug("Disposing.");
        mSetupDone = false;
        if (mServiceConn != null) {
            logDebug("Unbinding from service.");
            if (mContext != null) mContext.unbindService(mServiceConn);
        }
        mDisposed = true;
        mContext = null;
        mServiceConn = null;
        mService = null;
        mPurchaseListener = null;
    }

我应该怎么做才能解决这个错误? 我知道我应该捕获和异常,但我没有足够的信心自己改变这个类中的这个方法。

(感谢您的帮助)

最佳答案

经过更多研究,我发现这个问题已经被提出并得到回答。 不幸的是,该问题仍然被标记为未回答。 这里有link to the original question .

解决方案很简单:

  1. 您可以从指南中获取的文件已过时,应从 github 下载.
  2. 在方法 onDestroy 中,您应该使用以下代码:

    @Override
    public void onDestroy() {
       super.onDestroy();
       //Always unbind the connection with the store, otherwise performance degradation of the device may follow.
       if (mHelper != null) {
          mHelper.disposeWhenFinished();
          mHelper = null;
       }
    }
    

disposeWhenFinished 这是一个更优雅的解决方案,比 dispose 效果更好。

关于java - IabHelper(类)Dispose(方法)未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43180273/

相关文章:

java - web项目中如何使用spark mllib

android - android 上的 cordova-plugin-network-information 返回 connection.type = NONE 即使有 4G 连接

java - onCreate 中声明的随机整数

java - Java中生成更小的字符串的加密技术

java余弦相似度问题

java - 对于什么都不做的方法,您为 javadoc 编写了什么?

java - 如何从声明 edittext 的另一个类中的线程更改 edittext 的文本?

android - 左侧操作栏 Logo 填充

android - LogCat 不显示 TAG "SMS"

java - Android:循环遍历字符串数组 - Intent 方法的逻辑需要帮助