簡體   English   中英

獲取 Android 應用程序內購買項目的價格以顯示給用戶

[英]Get price of in-app purchase item for android app to display to the user

我正在使用以下庫在我的 android 應用程序中集成應用程序購買:

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

我想獲取要向用戶顯示的應用內商品的價格。 如果我使用庫中的getPurchaseListingDetails()方法,則必須返回一個List<SkuDetails> ,我可以從中提取價格。 但是我的代碼只返回一個空值。

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);處得到一個空指針異常priceButton01.setText(purchaseListingDetails.get(0).currency + purchaseListingDetails.get(0).priceValue); 這一行是因為purchaseListingDetails為空。

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

這是因為您輸入的產品 ID 無效。 產品 ID 應該類似於"com.anjlab.test.iab.s2.p5"

這個答案來晚了,但看到它沒有被接受的答案,我會給出一個可能的解決方案:

  • 您的產品 ID(“level_unlock”等)似乎有效
  • 嘗試將您的代碼放在以下方法下:

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

你需要執行這段代碼:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM