【发布时间】:2015-12-03 03:08:33
【问题描述】:
我能够在我的应用中成功购买应用内计费项目,但我还无法成功检查用户购买了哪些项目,因为我在此行收到空指针异常:
ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
我确实看到了有关此主题的其他帖子,但这些解决方案要么对我不起作用,要么我不完全理解解决方案:
-
getPurchases() NullPointerException initializing mService
-
In App Null Pointer Exception
- Android In-App billing: Null Pointer Exception
这是整个方法:
private void checkOwnedItems() throws RemoteException {
Bundle ownedItems;
String sku = "";
ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
if (purchaseDataList.size() > 0) {
//user owns 1 or more items
for (int i = 0; i < purchaseDataList.size(); ++i) {
sku = ownedSkus.get(i);
}
Toast.makeText(SettingsActivity.this, "You own these features: " + sku, Toast.LENGTH_LONG).show();
} else {
//user owns zero items, launch purchase flow
MACAddress = UniqueID.getMACAddress("wlan0");
int requestCode = 22222;
mHelper.launchPurchaseFlow(SettingsActivity.this, productID, requestCode, mPurchaseFinishedListener, MACAddress);
}
}
}
我运行了调试器,mService 似乎为空。我应该在哪里以及如何初始化 mService?
目前我正在尝试在 onServiceConnected 方法中初始化 mService,但也许永远不会调用 onServiceConnected。我应该何时以及如何调用 onServiceConnected 方法?
ServiceConnection mServiceConn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
};
【问题讨论】:
-
仅供参考,我正在尝试按照此处 Google 开发人员文档中的说明,在“查询可供购买的项目”部分下developer.android.com/google/play/billing/… 如果您希望我从应用程序中发布更多代码,或者回答更多问题,我很乐意这样做。
标签: android in-app-purchase in-app-billing