Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin.InAppBilling - NullReferenceException

$
0
0

I am trying to use the in-app billing component in my application. When trying to get the user's purchases, I am getting a NullReferenceException. It seems to be happening in the library and not in my code.

I was able to run the sample without issue, so it's something to do with my setup that's causing the component to crash. I've tried to model my code based on the sample, but that doesn't seem to help.

My in-app purchase is an "unlock" purchase. I only have one product, so my code basically just needs to check if the user has purchased anything.

I am running my code on a 2012 Nexus 7 running Android 4.2.2. I am using an account that I have setup to be a beta account for my application. The account has purchased the product.

protected override void OnCreate (Bundle bundle)
{
    StartServiceConnection ();
    base.OnCreate (bundle);

    // ... other code ...
}

private void StartServiceConnection()
{
    string value = Security.Unify (
        new string[] { "<My Key>", 
        "<Broken>", 
        "<into>", 
        "<4 pieces>" }, 
        new int[] { 0,1,2,3 });

    // Create a new connection to the Google Play Service
    _serviceConnection = new InAppBillingServiceConnection (this, value);
    _serviceConnection.OnConnected += () => {

        CheckUnlockService();
    };

    // Attempt to connect to the service
    _serviceConnection.Connect ();
}

private void CheckUnlockService()
{
    // if it's already unlocked, don't try again
    if (CheckIfUnlocked ())
        return;

    _isUnlocked = HasPurchasedUnlock ();

    // do stuff if unlocked
}

private bool HasPurchasedUnlock()
{
    try
    {
        if (_serviceConnection == null || _serviceConnection.BillingHandler == null)
        {
            Log.Error(_applicationName, "CheckUnlockService - BillingHandler is null");
            return false;
        }

        var ownedItems = _serviceConnection.BillingHandler.GetPurchases(ItemType.Product);
        if (ownedItems != null && ownedItems.Count > 0 && ownedItems[0].PurchaseState == 0)
            return true;
        else
            return false;
    }
    catch(Exception ex)
    {
        Log.Error(_applicationName, "CheckUnlockService threw exception: " + ex.Message + "\n\n" + ex.StackTrace);
        return false;
    }
}

Here is the stack trace that I get from the error...

Error(1661)/MyApp: CheckUnlockService threw exception: Object reference not set to an instance of an object
Error(1661)/MyApp: 
Error(1661)/MyApp:   at Xamarin.InAppBilling.InAppBillingHandler.GetPurchases (System.String itemType) [0x00000] in <filename unknown>:0 
Error(1661)/MyApp:   at MyApp.MainActivity.HasPurchasedUnlock () [0x00000] in <filename unknown>:0

I had to add all the error handling to keep my app from crashing. I'm not really sure what's going on. I've been dealing with this for almost a week now and have become very frustrated.

Any ideas?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>