【发布时间】:2017-09-11 12:30:42
【问题描述】:
我收到以下错误:
java.lang.NullPointerException:尝试调用接口方法 android.os.Bundle com.android.vending.billing.IInAppBillingService.getSkuDetails(int, java.lang.String, java.lang.String, android.os.Bundle) 在空对象引用上
当我点击我的按钮时:
skuDetails = billingService.getSkuDetails(3, "com.android.vending.billing", "inapp", querySkus);
所有参数都没有空值,但同时我得到了这样的错误。
完整代码如下:
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
billingService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
billingService = IInAppBillingService.Stub.asInterface(service);
}
};
Intent intent = new Intent("com.android.vending.billing.IInAppBillingService.BIND");
intent.setPackage("com.android.vending.billing");
getBaseContext().bindService(intent, connection, Context.BIND_AUTO_CREATE);
buyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> skuList = new ArrayList<String>();
skuList.add(ITEM_SKU);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Log.e(TAG, "onClick: " + querySkus.getStringArrayList("ITEM_ID_LIST"));
Bundle skuDetails = null;
try {
skuDetails = billingService.getSkuDetails(3,
"com.android.vending.billing",
"inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> responseList = skuDetails
.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals(ITEM_SKU)) {
System.out.println("price " + price);
Bundle buyIntentBundle = billingService
.getBuyIntent(3, getPackageName(), sku,
"inapp",
base64EncodedPublicKey);
PendingIntent pendingIntent = buyIntentBundle
.getParcelable("BUY_INTENT");
startIntentSenderForResult(
pendingIntent.getIntentSender(), 1001,
new Intent(), Integer.valueOf(0),
Integer.valueOf(0), Integer.valueOf(0));
}
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
【问题讨论】:
标签: java android in-app-purchase in-app-billing