2015-11-28 38 views
1

Note:我主要在這裏發佈這個問題,這樣它的谷歌索引和備用一些可憐的同胞可以深入挖掘陷入這混亂的困境只發現這不是他們自己的錯。這個問題的可接受答案是我可以使用的解決方法,或者Xamarin/SimpleJson有人認爲這是一個錯誤,它會被修復。Xamarin.InAppBilling - 「Error available inventory:System.NullReferenceException」

我已經成功使用Xamarin.InAppBilling一年多了(在真正的Google Play商店上購物並正常工作)。最近,我們開始重新測試該功能(由於重構)和(使用靜態響應測試PRODUKT SKU的時候連)查詢我們的產品庫存時會收到此異常:

Error Available Inventory: System.NullReferenceException: Object reference not set to an instance of an object 
    at Xamarin.InAppBilling.PocoJsonSerializerStrategy.DeserializeObject (System.Object value, System.Type type) [0x0047a] in <filename unknown>:0 
    at Xamarin.InAppBilling.SimpleJson.DeserializeObject (System.String json, System.Type type, IJsonSerializerStrategy jsonSerializerStrategy) [0x0003d] in <filename unknown>:0 
    at Xamarin.InAppBilling.SimpleJson.DeserializeObject[T] (System.String json) [0x00000] in <filename unknown>:0 
    at System.Linq.Enumerable+WhereSelectEnumerableIterator`2[TSource,TResult].MoveNext() [0x00064] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:285 
    at System.Collections.Generic.List`1[T]..ctor (IEnumerable`1 collection) [0x0008b] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:105 
    at System.Linq.Enumerable.ToList[TSource] (IEnumerable`1 source) [0x00011] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:835 
    at Xamarin.InAppBilling.InAppBillingHandler+<QueryInventoryAsync>c__AnonStorey0.<>m__0() [0x00092] in <filename unknown>:0 

我已經扭轉Xamarin的在應用內結算代碼,特別是Xamarin.InAppBilling.InAppBillingHandler.QueryInventoryAsync(IList<string> skuList, string itemType)方法。

它的功能是查詢Google Play Billing API中的sku細節,然後反序列化返回的​​json對象。這是代碼拋出NullReferenceException的地方。所有這些都超出了我的控制範圍,所以我使用了此代碼的修改副本來獲取原始json,並確信它對我來說非常好。代碼:

var service = _serviceConnection.Service; 
Task.Factory.StartNew<IList<Product>>(() => { 
    IList<Product> result; 
    try 
    { 
     Bundle bundle = new Bundle(); 
     bundle.PutStringArrayList("ITEM_ID_LIST", skus); 
     Bundle skuDetails = service.GetSkuDetails(3, this.Activity.PackageName, ItemType.Product, bundle); 
     int responseCode = skuDetails.GetInt("RESPONSE_CODE"); 
     if (responseCode != 0) 
     { 
      ViewModel.ShowTransactionError("Error querying inventory: " + TranslateResponseCode(responseCode)); 
      result = null; 
     } 
     else 
     { 
      IList<string> stringArrayList = skuDetails.GetStringArrayList("DETAILS_LIST"); 
      result = null; 
      if (stringArrayList != null) 
      { 
       Console.WriteLine("DETAILS_LIST: " + string.Join("\n - ", stringArrayList)); 
       result = stringArrayList.Select(x => JsonConvert.DeserializeObject<Product>(x)).ToList(); 
      }       
     } 
    } catch (Exception ex) 
    { 
     ViewModel.ShowTransactionError("Error querying inventory: " + ex.ToString()); 
     result = null; 
    } 
    return result; 
}) 

JSON:

DETAILS_LIST: {"title":"Sample Title","price":"0,93 €","type":"inapp","description":"Sample description for product: android.test.canceled.","price_amount_micros":933038,"price_currency_code":"EUR","productId":"android.test.canceled"} 
- {"title":"Sample Title","price":"0,93 €","type":"inapp","description":"Sample description for product: android.test.item_unavailable.","price_amount_micros":933038,"price_currency_code":"EUR","productId":"android.test.item_unavailable"} 
- {"title":"Sample Title","price":"0,93 €","type":"inapp","description":"Sample description for product: android.test.purchased.","price_amount_micros":933038,"price_currency_code":"EUR","productId":"android.test.purchased"} 
- {"title":"Sample Title","price":"0,93 €","type":"inapp","description":"Sample description for product: android.test.refunded.","price_amount_micros":933038,"price_currency_code":"EUR","productId":"android.test.refunded"} 
- {"title":"Boat Upgrade (Rowing in Motion - Solo)","price":"69,90 €","type":"inapp","description":"Analyze and Record an unlimited number of strokes per rowing session.","price_amount_micros":69900000,"price_currency_code":"EUR","productId":"com.rowinginmotion.mobile.boatapp.droid.solo.boat"} 
- {"title":"Coach Upgrade (Rowing in Motion - Solo)","price":"98,77 €","type":"inapp","description":"Receive unlimited live data from a boat using a mobile WiFi.","price_amount_micros":98770000,"price_currency_code":"EUR","productId":"com.rowinginmotion.mobile.boatapp.droid.solo.coach"} 

,所以我想的東西是錯誤與SimpleJson/PocoJson任何串行他們在那裏有。看起來像是圖書館裏的一個bug,而不是我身邊的錯誤(通過他們的清單和所有看上帝的東西)。

我會使用JSON.NET問題是他們的Product類顯然沒有默認的構造函數(什麼?!,我已經與IL工作了很多年,從未見過這樣的東西),但有一個CompilerGeneratedAttribute卡住它。我的產品將需要是該類的實例,但我可以通過它們回到它們的API,但...

回答

2

linker再次點擊。 PocoJsonSerializerStrategy使用了大量的反思,所以這是我的第二個猜測,在通過長列表things you need to get right for IAPs to work

添加到您的.csproj,你會很好去。

<AndroidLinkSkip>Xamarin.InAppBilling</AndroidLinkSkip> 

這是一個真正的失望Xamarin不使自己的庫發生鏈接器的證明......

這個問題很容易花了我一兩天,特別是因爲有這麼多的subtle ways,其中在 - App-Billing可能會打破你應該首先檢查的問題(特別是,我們將大部分IAP邏輯從活動轉移到片段,因此也必須進行測試)。

旁註:我徒勞地等待3x2h,因爲我的測試設備上可以使用Google Play Alpha通道更新我的儀器化版本。對於我可以使用Xamarin Studio部署的構建(甚至是調試構建)成功地測試和運行IAP。這並不意味着這也適用於你。

+0

錯誤報告爲:https://bugzilla.xamarin.com/show_bug.cgi?id = 36355 –

相關問題