java - disposeWhenFinished() 导致 API 21 崩溃

标签 java android in-app-purchase

以下 onDestroy 方法使我的应用程序在 API 21 上运行时崩溃(例如,当我旋转屏幕时):

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mHelper != null) {
        mHelper.disposeWhenFinished();
        mHelper = null;
    }
}

该方法来自 Google 在其应用内结算教程中提供的 IabHelper 类。

/**
 * Disposes of object, releasing resources. If there is an in-progress async operation, this
 * method will queue the dispose to occur after the operation has finished.
 */
public void disposeWhenFinished() {
    synchronized (mAsyncInProgressLock) {
        if (mAsyncInProgress) {
            logDebug("Will dispose after async operation finishes.");
            mDisposeAfterAsync = true;
        } else {
            try {
                dispose();
            } catch (IabAsyncInProgressException e) {
                // Should never be thrown, because we call dispose() only after checking that
                // there's not already an async operation in progress.
            }
        }
    }
}

这是错误消息:

 java.lang.RuntimeException: Unable to destroy activity {package name}: java.lang.IllegalArgumentException: Service not registered: packagename.util.IabHelper$1@3bf48617

我找不到解决方案,这让我很奇怪,因为应用内结算需要这种方法。

最佳答案

您需要在 onDestroy() super 调用之前或 onPause() 中的某个位置释放 IabHelper,以确保您的 Activity 将使用 isFinishing() 方法完成,因为无法保证 onDestroy() super 调用之后的代码将被执行。

应在 Activity 上下文与播放计费服务解除绑定(bind)之前完成处置。您收到该消息是因为当您的应用程序释放了服务连接时,您可能会遇到这种情况。

关于java - disposeWhenFinished() 导致 API 21 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301626/

相关文章:

Android - 应用内购买 - 您需要将 BILLING 权限添加到您的 APK

java - JavaFX View 初始化时的空指针

java - Spring Boot Maven 集成测试给出 ClassNotFoundException

android - hostapd 和 rpbi 强制门户

java - 无法恢复 Android 默认音乐播放器

android - Amazon IAP v2.0 不响应 android 实时测试

iphone - 应用内购买产品 ID

java - 如何从firebase实时数据库获取数据

类似于 wordexp 的 Java shell 扩展

android - 如何在 Observables.combineLatest 中监听三个以上的字段