Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin - Android - Plugin.InAppBilling Exception

I'm trying to implement (In App Billing) for Xamarin forms solution by using plugin InAppBilling
I couldn't find any full code example or documentation for this plugin except this one
I follow the instruction step by step but when i run my code I got this exception

{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 }

My sample code
This code in (Shared/Portable Class Libraries/.NET Standard)

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

            }
}

And This code in 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);
        }
    }
like image 612
ASalameh Avatar asked Sep 23 '18 18:09

ASalameh


1 Answers

You are missing a part of the documention where you set the current activity (context) in the OnCreate method:

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
like image 98
SushiHangover Avatar answered Nov 11 '22 06:11

SushiHangover