I am able to successfully purchase in-app billing items in my app, but I have not yet been able to successfully check which items the user has purchased, as I am getting a a null pointer exception on this line:
ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
I did see the other posts on this topic, but those solutions either did not work in my case, or I don't fully understand the solution:
- getPurchases() NullPointerException initializing mService
- In App Null Pointer Exception
- Android In-App billing: Null Pointer Exception
Here is the entire method:
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);
}
}
}
I ran the debugger, and it appears that mService is null. Where and how am I supposed to initialize mService?
Currently I am trying to initialize mService in the onServiceConnected method, but perhaps onServiceConnected is never getting called. When and how should I be calling the onServiceConnected method?
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;
}
};