以下 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)之前完成处置。您收到该消息是因为当您的应用程序释放了服务连接时,您可能会遇到这种情况。
我是一名优秀的程序员,十分优秀!