gpt4 book ai didi

c# - Xamarin - Android - Plugin.InAppBilling 异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:49 24 4
gpt4 key购买 nike

我正在尝试通过使用 plugin InAppBilling 为 Xamarin 表单解决方案实现(应用内计费)
除了this one,我找不到这个插件的任何完整代码示例或文档。
我一步一步地按照说明操作,但是当我运行我的代码时,我得到了这个异常

{System.NullReferenceException: Current Context/Activity is null, ensure that the MainApplication.cs file is setting the CurrentActivity in your source code so the In App Billing can use it. at Plugin.InAppBilling.InAppBillingImplementation.get_Context () [0x00000] in C:\projects\inappbillingplugin\src\Plugin.InAppBilling.Android\InAppBillingImplementation.cs:59 at Plugin.InAppBilling.InAppBillingImplementation.ConnectAsync (Plugin.InAppBilling.Abstractions.ItemType itemType) [0x00000] in C:\projects\inappbillingplugin\src\Plugin.InAppBilling.Android\InAppBillingImplementation.cs:372 at myproject.MainPage+d__28.MoveNext () [0x00051] in C:\Users\xuser\source\repos\myproject\MainPage.xaml.cs:415 }

我的示例代码
此代码在(共享/可移植类库/.NET 标准)

MainPage.xaml.cs

 public partial class MainPage : ContentPage
{

void OnPurchased(object sender, EventArgs e)
{
InAppBilling();
}
async void InAppBilling()
{
try
{
var productId = "xxxxx.xxxx_appbilling";
var connected = await CrossInAppBilling.Current.ConnectAsync();
if (!connected)
{
//Couldn't connect to billing, could be offline, alert user
return;
}
//try to purchase item
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload");
if (purchase == null)
{
//Not purchased, alert the user
}
else
{
//Purchased, save this information
var id = purchase.Id;
var token = purchase.PurchaseToken;
var state = purchase.State;
}
}
catch (Exception ex)
{
//Something bad has occurred, alert user
}
finally
{
//Disconnect, it is okay if we never connected
await CrossInAppBilling.Current.DisconnectAsync();
}

}
}

Droid 项目中的这段代码

 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
}
}

最佳答案

您缺少在 OnCreate 方法中设置当前 Activity (上下文)的文档的一部分:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

Android Current Activity Setup

This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity’s OnCreate method:

关于c# - Xamarin - Android - Plugin.InAppBilling 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com