我正在学习如何根据这篇文章实现应用内结算 - http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80%93_A_Tutorial
每当我尝试在DEBUG模式下运行此应用时,我收到错误,指出“Google Play商店”已崩溃,我的日志显示
E / InAppBilling:应用内结算错误:IAB活动结果中的空数据。
错误:购买错误:labResult:IAB结果中的空数据(响应:-1002:收到错误的响应)
引起:java.lang.NullPointerException:尝试调用接口方法'android.os.Bundle com.android.vending.billing.IInAppBillingService.getBuyIntent(int,java.lang.String,java.lang.String,java 。null对象引用上的.lang.String,java.lang.String)'
此外,我已经在RELEASE模式下成功运行了名为Trivial Drive的谷歌样本,但是当我用测试版替换真正的SKU(例如android.test.purchased)并在DEBUG模式下运行app时,我得到了同样的错误。
完整活动代码
public class InAppBillingActivity extends AppCompatActivity {
private Button clickButton;
private Button buyButton;
private static final String TAG = "InAppBilling";
IabHelper mHelper;
static final String ITEM_SKU = "android.test.purchased";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.billing_activity);
buyButton = (Button)findViewById(R.id.buyButton);
clickButton = (Button)findViewById(R.id.clickButton);
clickButton.setEnabled(false);
String base64EncodedPublicKey = "HERE IS MY KEY FROM DEVELOPER CONSOLE";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed:" + result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
mHelper.enableDebugLogging(true, TAG);
}
}
});
}
public void buttonClicked (View view) {
clickButton.setEnabled(false);
buyButton.setEnabled(true);
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {// Handle error
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
buyButton.setEnabled(false);
}
}
};
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
clickButton.setEnabled(true);
} else {
// handle error
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
答案 0 :(得分:3)
嘿,这是因为新的谷歌游戏商店更新而发生的 你做的每件事都很完美
因此没有必要对此崩溃做任何事情。只需等待Google Play商店应用中的错误修复。
有关模式详细信息,您可以查看此question
编辑:2017年8月11日
现在新的谷歌游戏商店版本 8.0.73.R-所有[0] [公关] 162689464 谷歌修复此崩溃问题我有在我的设备上成功测试了它,所以现在你不再遇到这个问题了。 :)
答案 1 :(得分:0)
从您的日志中我认为您发送了错误的参数。 在我的申请中
对于托管产品,我使用这些参数
mHelper.launchPurchaseFlow(this, SKU, PURCHASE_REQUEST, mPurchaseFinishedListener, payload);
对于订阅产品,我使用这些参数
mHelper.launchPurchaseFlow(this, SKU, IabHelper.ITEM_TYPE_SUBS, oldSkus, PURCHASE_REQUEST, mPurchaseFinishedListener, payload);
尝试这些可能对你有帮助..