获取Android应用的应用内购买商品的价格,以便向用户展示

时间:2017-06-12 13:35:29

标签: android nullpointerexception in-app-purchase

我使用以下库在我的Android应用中集成应用内购买:

https://github.com/anjlab/android-inapp-billing-v3#getting-listing-details-of-your-products

我想获取要向用户显示的应用内商品的价格。如果我使用库中的getPurchaseListingDetails()方法,则必须返回List<SkuDetails>,我可以从中提取价格。但是我的代码只返回null。

BillingProcessor bp;

ArrayList<String> arrayListOfProductIds;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    arrayListOfProductIds = new ArrayList<>();

    arrayListOfProductIds.add("level_unlock");
    arrayListOfProductIds.add("hint_buy");
    arrayListOfProductIds.add("hint_level");

    boolean isAvailable = BillingProcessor.isIabServiceAvailable(this);
    if(isAvailable) {
        bp = BillingProcessor.newBillingProcessor(this, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz0mDL13z6FtY0Xy09ru0LST4pgy4ph6smeHEsaFbX8fZYVR6SM+eHSIA3j+DnUhiRK8QTun/VJdRnbovvX25rrMju3unAqJzur5whB/4OSXoh6YLOp5+UBiAPuQEzOFsmVhJlwZVH8ScjjwveqXmkByaTde4Ca/FrSoQpbFcuGY0BJPfSSxaQzjs9pwbhNpUqAXF3pbay4/B4o0tw9+rK/8qcGW2ZbG2V/VPQzMZKKBKfyCogY2YV37q80s8CP3tqBzbbnLOF9pmji3A/iuRheclfVuFjj1p7keXMUch2kPPrRJVtLcDbR8gBG0Gju/27zCIaFzFG2RWeu45Qkw5uQIDAQAB", this);
        bp.initialize();
    }

    LinearLayout backButtonShop = (LinearLayout) findViewById(R.id.back_button_shop);
    backButtonShop.setOnClickListener(this);

    Button priceButton01 = (Button) findViewById(R.id.price_button_01);
    priceButton01.setOnClickListener(this);

    Button priceButton02 = (Button) findViewById(R.id.price_button_02);
    priceButton02.setOnClickListener(this);

    Button priceButton03 = (Button) findViewById(R.id.price_button_03);
    priceButton03.setOnClickListener(this);

    List<SkuDetails> purchaseListingDetails = bp.getPurchaseListingDetails(arrayListOfProductIds);
    priceButton01.setText(purchaseListingDetails.get(0).currency + purchaseListingDetails.get(0).priceValue);
    priceButton02.setText(purchaseListingDetails.get(1).currency + purchaseListingDetails.get(1).priceValue);
    priceButton03.setText(purchaseListingDetails.get(2).currency + purchaseListingDetails.get(2).priceValue);

    }

}

我在priceButton01.setText(purchaseListingDetails.get(0).currency + purchaseListingDetails.get(0).priceValue);获得空指针异常 这一行因为purchaseListingDetails为空。

如何让它返回List<SkuDetails>而不是null?

3 个答案:

答案 0 :(得分:0)

这是因为您输入的产品ID无效。产品ID应如下"com.anjlab.test.iab.s2.p5"

答案 1 :(得分:0)

这个答案很晚,但是看到它没有被接受的答案,我将给出一个可能的解决方案:

  • 您的产品ID(“ level_unlock”等)确实有效
  • 尝试将您的代码置于以下方法下:

    @Override
    public void onBillingInitialized() {
    <your code here>
    }
    

答案 2 :(得分:0)

您需要执行以下代码段:

List<SkuDetails> purchaseListingDetails = bp.getPurchaseListingDetails(arrayListOfProductIds);
    priceButton01.setText(purchaseListingDetails.get(0).currency + purchaseListingDetails.get(0).priceValue);
    priceButton02.setText(purchaseListingDetails.get(1).currency + purchaseListingDetails.get(1).priceValue);
    priceButton03.setText(purchaseListingDetails.get(2).currency + purchaseListingDetails.get(2).priceValue);

onBillingInitialized()方法内,而不在onCreate()中。