编程语言
首页 > 编程语言> > C#-Xamarin-Android-Plugin.InAppBilling异常

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

作者:互联网

我正在尝试通过使用plugin InAppBilling为Xamarin表单解决方案实施(In App Billing),我找不到该插件的任何完整代码示例或文档,除了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 Project中的这段代码

 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方法中设置当前活动(上下文)的文档部分:

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:

> https://jamesmontemagno.github.io/InAppBillingPlugin/GettingStarted.html

标签:xamarin,xamarin-android,xamarin-forms,c,android
来源: https://codeday.me/bug/20191108/2008686.html