Android Question Unable to create a class based on the icon menu badge example

toby

Well-Known Member
Licensed User
Longtime User
I'm trying to create a class based on this example:

menu badge class file:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private CartBitmap As B4XBitmap
    Private IconCanvas As B4XCanvas
    Private mBadgeNumber As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(root1 As B4XView, BadgeNumber As Int)
    Root=root1
    mBadgeNumber=BadgeNumber
    CartBitmap = xui.LoadBitmap(File.DirAssets, "cart.png")
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 32dip, 32dip)
    IconCanvas.Initialize(p)
End Sub


Public Sub UpdateMenuItems
   
    Dim bmp As B4XBitmap = CreateIconWithBadge(CartBitmap, mBadgeNumber)
    #if B4A
    Dim menus As List = B4XPages.GetManager.GetPageInfoFromRoot(Root).Parent.MenuItems
    menus.Clear
    'add menu items
    Dim mi As B4AMenuItem = B4XPages.AddMenuItem(Root, "cart")
    mi.AddToBar = True
    mi.Bitmap = bmp
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    ctxt.RunMethod("invalidateOptionsMenu", Null)
    #else if B4i
    Dim bb As BarButton
    bb.InitializeBitmap(Main.KeepOriginalColors(bmp), "cart")
    B4XPages.GetNativeParent(Me).TopRightButtons = Array(bb)
    #end if
End Sub

Private Sub CreateIconWithBadge(bmp As B4XBitmap, Number As Int) As B4XBitmap
    IconCanvas.ClearRect(IconCanvas.TargetRect)
    IconCanvas.DrawBitmap(bmp, IconCanvas.TargetRect)
    Number = Min(Number, 9)
    If Number > 0 Then
        Dim cx As Int = IconCanvas.TargetRect.Width - 8dip
        Dim cy As Int = 8dip
        IconCanvas.DrawCircle(cx, cy, cy, Colors.Red, True, 0)
        Dim fnt As B4XFont = xui.CreateDefaultBoldFont(12)
        Dim r As B4XRect = IconCanvas.MeasureText(Number, fnt)
        Dim BaseLine As Int = cy - r.Height / 2 - r.Top
        IconCanvas.DrawText(Number, cx, BaseLine, fnt, xui.Color_White, "CENTER")
    End If
    Return IconCanvas.CreateBitmap
End Sub

public Sub increment
    mBadgeNumber=mBadgeNumber +1
    UpdateMenuItems
End Sub

public Sub decrement
    mBadgeNumber=mBadgeNumber-1
    UpdateMenuItems
End Sub

public Sub Clear
    mBadgeNumber=0
    UpdateMenuItems
End Sub


And it fails while executing following line of code
Root variable is from page2's Root:
Dim mi As B4AMenuItem = B4XPages.AddMenuItem(Root, "cart")
error logs:
Error occurred on line: 312 (B4XPagesManager)
java.lang.NullPointerException: Attempt to read from field 'com.ddg.menu.badge.b4xpagesmanager$_b4xpageparent com.ddg.menu.badge.b4xpagesmanager$_b4xpageinfo.Parent' on a null object reference
    at com.ddg.menu.badge.b4xpagesmanager._addmenuitem(b4xpagesmanager.java:224)
    at com.ddg.menu.badge.b4xpages._addmenuitem(b4xpages.java:81)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.ddg.menu.badge.clscartmenubadge._updatemenuitems(clscartmenubadge.java:88)
    at com.ddg.menu.badge.page2._b4xpage_created(page2.java:77)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1058)
    at com.ddg.menu.badge.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1082)
    at com.ddg.menu.badge.b4xpagesmanager._showpage(b4xpagesmanager.java:439)
    at com.ddg.menu.badge.b4xpages._showpage(b4xpages.java:55)
    at com.ddg.menu.badge.b4xmainpage._button1_click(b4xmainpage.java:188)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:8160)
    at android.widget.TextView.performClick(TextView.java:16222)
    at android.view.View.performClickInternal(View.java:8137)
    at android.view.View.access$3700(View.java:888)
    at android.view.View$PerformClick.run(View.java:30236)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8550)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Could someone kindly tell me what I did wrong, please?

TIA
 

Attachments

  • menuBadgeClass.zip
    19.5 KB · Views: 142
Last edited:

toby

Well-Known Member
Licensed User
Longtime User
This is the code of page2 that uses the class:
page2 code:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Dim CartMenuBadge As clsCartMenuBadge
    Private btnClear As Button
    Private btnDecrement As Button
    Private btnIncrement As Button
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("Page2")
    CartMenuBadge.Initialize(Root, 0)
    CartMenuBadge.UpdateMenuItems
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub btnIncrement_Click
    CartMenuBadge.increment
End Sub

Private Sub btnDecrement_Click
    CartMenuBadge.decrement
End Sub

Private Sub btnClear_Click
    CartMenuBadge.Clear
End Sub
that uses the class
 
Upvote 0
Top