我正在尝试在我最新的 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 .
解决方案很简单:
- 您可以从指南中获取的文件已过时,应从 github 下载.
在方法
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/