Android Question In-App billing error

jtare

Active Member
Licensed User
Longtime User
I implemented promo codes in my app, the user redeems the promo code in Play Store.
When the app starts, each time it have to ask for the owned products to check if the user entered or not the promo code. From the user's perspective, after redeeming the code in the play store the changes have to apply as soon the user opens the app again.

This is the code I use:
B4X:
Sub Activity_Create(FirstTime As Boolean)
        If FirstTime Then
            manager.Initialize("manager", inappkey)
            manager.DebugLogging = False
        Else
            Try
                manager.GetOwnedProducts
            Catch
'                Log(LastException)
            End Try
        End If
End Sub

since there is no way to tell if the manager is initialized I assume that if Firstime = false it is already initialized, I added a try catch block to protect that part of the code anyways.

Im not sure which part of the code is causing this issue but I get this error: (As soon as I opened the app, the app doesn't even launch completely)
B4X:
java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getPurchases(int, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
    at anywheresoftware.b4a.objects.IbHelper.queryPurchases(IbHelper.java:836)
    at anywheresoftware.b4a.objects.IbHelper.queryInventory(IbHelper.java:544)
    at anywheresoftware.b4a.objects.IbHelper.queryInventory(IbHelper.java:523)
    at anywheresoftware.b4a.objects.IbHelper$3.run(IbHelper.java:616)
    at java.lang.Thread.run(Thread.java:762)

Someone knows what could be wrong? This error happens once in a while, from firebase crash report I noticed that very few users had this issue, for me it happened for the first time in almost a month.
Maybe there is another approach to check the owned products on each app start?
 

jtare

Active Member
Licensed User
Longtime User
Thanks for the reply, but I noticed that in your code, in the case that FirsTime = false it won't check the owned products.
Is there a way to check if the manager is initialized? So I can safely run the ownedproducts method in every app start or resume. I'm Not sure if this is the correct approach to look if the user redeemed or not a promo code.
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
For some reason my FirstTime is not always true.

I found what seems to be the solution here: https://developer.android.com/google/play/billing/billing_promotions.html , which says "The simplest approach is to call getPurchases() in your activity's onResume() method, since that callback fires when the activity is created, as well as when the activity is unpaused. Calling getPurchases() on startup and resume guarantees that your app finds out about all purchases and redemptions the user may have made while the app wasn't running. "

But how do I run the getPurchases() or getOwnedProducts if I don't know if the manager is initialized(assuming this is the reason for the error)?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

jtare

Active Member
Licensed User
Longtime User
I added
B4X:
Sub Process_Globals
    Private billingSupported As Boolean = False
End Sub
Sub Activity_Resume
'    Log("BillingSupported = "&billingSupported)
    Try
        If billingSupported Then manager.GetOwnedProducts
    Catch
        '                Log(LastException)
    End Try
End Sub
Sub manager_BillingSupported (Supported As Boolean, Message As String) 'First we check if Billing is supported
'    Log(Supported & ", " & Message)
'    Log("Subscriptions supported: " & manager.SubscriptionsSupported)
    billingSupported = Supported
    If Supported Then 
        manager.GetOwnedProducts 'if support true then get owend product by this user
    Else
'        Log("BILLLING NOT SUPPORTED")
    End If
End Sub
And seems to be working, thanks for the suggestion.
 
Upvote 0
Top