这是我的代码,尝试在按下按钮时启动应用内购买:
purchaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(purchased.equals("remove_ads")) {
Toast.makeText(getApplicationContext(), "You already own the item.",
Toast.LENGTH_LONG).show();
}
else{
try{
Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(),
"remove_ads", "inapp", "key");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
try{
startIntentSenderForResult(pendingIntent.getIntentSender(), ///NPE here
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
catch(IntentSender.SendIntentException ee){
Toast.makeText(getApplicationContext(), "Error was: " + ee,
Toast.LENGTH_LONG).show();
}
catch(NullPointerException n){
Toast.makeText(getApplicationContext(), "Error was: " + n,
Toast.LENGTH_LONG).show();
}
}
catch (RemoteException e){
Toast.makeText(getApplicationContext(), "Error was: " + e,
Toast.LENGTH_LONG).show();
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(store.this, "remove_ads", 10001,
mPurchaseFinishedListener, "key");
}
}
}
});
由于某种原因,我在指示startIntentSenderForResult...
的行上获得了NPE,但我不明白可能导致它的原因。以前我在应用程序购买中使用此代码完全如所示,除了它是一个不同的sku。它是否会有所不同,因为我在同一个类中显示了此代码块的2个相同副本? Stacktrace也没有显示任何有用的东西,只有NPE。