I get the following exception when I use PurchaseAsync in my Android project:
var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase);
excep {System.NullReferenceException: Current Activity is null, ensure that the MainActivity.cs file is configuring Xamarin.Essentials in your source code so the In App Billing can use it. at Plugin.InAppBilling.InAppBillingImplementation.get_Activity() in D:\a\1\…}
But I don't use Xamarin.Essentials anymore. I migrated from In-App Billing Plugin 4.0.2 to 6.7.0 and I use Microsoft.Maui.Essentials now. I have only tested In-App Billing Plugin 6.7.0 in my Android project, I have not yet tested in my iOS project.
What should I do in my Activity1.cs?
protected override void OnCreate(Bundle bundle)
{
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
// Xamarin.Essentials.Platform.Init(this, bundle); old code from version 4.0.2
What should I do to avoid the exception?
CodePudding user response:
The Xamarin.Essentials is for the Xamarin.Forms project, and for the Maui project, you can try to use the following code in the MainActivity or the Activity1:
protected override void OnCreate(Bundle bundle)
{
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
Microsoft.Maui.ApplicationModel.Platform.Init(this,bundle);
}
Or you can use the following code in the MainActivity or the Activity1 according to the plugin document on the github:
protected override void OnCreate(Bundle bundle)
{
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;
var context = Microsoft.Maui.ApplicationModel.Platform.AppContext;
}
In addition, there is a similar issue about some errors after migrating from 4.0.2 to 6.7.0 on the github, you can follow up it.