Android Question B4X Canvas Customview - null object

Alpay ABAY

Member
Licensed User
Hi there,

I am a newbie with B4X. I worked with canvas object with several languages before.
I have problem when I try to use canvas inside of a custom view.

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

How I should use safely canvas object inside a customview ?

B4X:
'Custom View class
#DesignerProperty: Key: propSpacing, DisplayName: Spacing, FieldType: Int, DefaultValue: 5, Description: General spacing
#DesignerProperty: Key: propBackground, DisplayName: Background, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: General background
#DesignerProperty: Key: propGraphFrameBackground, DisplayName: FrameBackground, FieldType: Color, DefaultValue: 0xFF00B6FF, Description: Graph frame background
#DesignerProperty: Key: propGraphBackground, DisplayName: Background, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: Graph background
#DesignerProperty: Key: propLevelColor, DisplayName: Level color, FieldType: Color, DefaultValue: 0xFF00DD19, Description: Level background
#DesignerProperty: Key: propLineColor, DisplayName: Line color, FieldType: Color, DefaultValue: 0xFF333333, Description: Line color
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Private mBase As Panel
    Private Const DefaultColorConstant As Int = -984833 'ignore
    Private dataMap As Map
    Private canvas As Canvas
    Private gPanel As Panel
    Private locked As Boolean = False
    Public dataSum As Float = 0
    
    Private propSpacing As Int
    
    Private propBackground As Int
    Private propGraphFrameBackground As Int
    Private propGraphBackground As Int
    Private propLevelColor As Int
    Private propLineColor As Int
    
    Private gLabel As Label
End Sub

Public Sub lock
    locked=True
End Sub

Public Sub unlock(aUpdateLayout As Boolean)
    locked=False
    If (aUpdateLayout=True) Then
        updateCanvas
    End If
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
    gPanel.Initialize("")
    canvas.Initialize(gPanel)
    
End Sub

Public Sub itemCount As Int
    Return dataMap.Size   
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, propS As Map)
    dataMap.Initialize
    mBase = Base
    mBase.Tag = Me
    Sleep(0)
    mBase.LoadLayout("layChartGraph")
    Sleep(0)
    propSpacing=propS.Get("propSpacing")
    propBackground=propS.Get("propBackground")
    propGraphFrameBackground=propS.Get("propGraphFrameBackground")
    propGraphBackground=propS.Get("propGraphBackground")
    propLevelColor=propS.Get("propLevelColor")
    propLineColor=propS.Get("propLineColor")
    
    
End Sub

Public Sub GetBase As Panel
    Return mBase
End Sub

Public Sub addItem(aLabel As String,aValue As Float)
    dataMap.Put(aLabel,aValue)
    dataSum=dataSum+aValue
    updateCanvas
End Sub

Public Sub removeItem(aLabel As String)
    If (dataMap.ContainsKey(aLabel)) Then
        Dim val As Float = dataMap.Get(aLabel)
        dataMap.Remove(aLabel)
        dataSum=dataSum-val
    End If
    updateCanvas
End Sub

Public Sub AddToParent(Parent As B4XView, Left As Int, Top As Int, Width As Int, Height As Int)
    mBase.Initialize("mbase")
    Parent.AddView(mBase,Left,Top,Width,Height)
End Sub

Public Sub updateCanvas
    If (locked=False) Then
        mBase.Color=propBackground
        gPanel.Color=propGraphFrameBackground
    
        
        Dim tR As Rect
        tR.Initialize(5dip,5dip,100dip,40dip)
        canvas.DrawRect(tR,0xFF444444,False,1)
    End If
End Sub
 

Alexander Stolte

Expert
Licensed User
Longtime User
gPanel.Initialize("") canvas.Initialize(gPanel)
You simply initialize a panel without specifying how large this panel is.

3 steps:
  1. declare your panel as B4XView
    1. Private gPanel As B4XView
  2. use gPanel = xui.CreatePanel("") to create the panel
  3. assign a size to the panel
    1. gPanel.SetLayoutAnimated(0,0,0,10dip,10dip)
Then it will work. But it only becomes visible when you assign a parent to the panel.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
It would be easier for us to help you if you posted your project as a zip file.
If it is a B4A only project use Export As Zip in the File menu.



If it is a B4XPages project, press the ctrl button and click on the link on top of the B4XMainPage module.

 
Upvote 0

Alpay ABAY

Member
Licensed User
My mistake. I think I made a mistake about module name or layout.
I re-created the component now it works properly.

Thanks for support
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…