Android Question Class not returning view

jvetterli

Member
Licensed User
Longtime User
Hi all,

I'm struggling to create a class who should act as a custom button (panel with label view):

Class gets initialized including the needed width and height:
B4X:
Public Sub Initialize(pEventname As String, pModule As Object, iWidth As Int, iHeight As Int)
   
    module    = pModule
    Eventname = pEventname
    BkgPanel.Initialize("pnl")
   
    btnWidth = iWidth
    btnHeight = iHeight
   
End Sub

Getting the view back as panel, at this time I'm trying to set the dimensions of the panel and label:

B4X:
Public Sub AsPanel As Panel
   
    Dim LblH As Int = btnWidth/3 + 5dip
    Dim PicSize As Int = btnHeight - LblH - 5dip
   
    BkgPanel.Left = 0
    BkgPanel.Top = 0
    BkgPanel.Width = btnWidth
    BkgPanel.Height = btnHeight
    BkgPanel.Color = Colors.Gray
   
    BtnLabel.Initialize("pnl")
    BtnLabel.Text = "TestTest"
    BtnLabel.TextColor = Colors.White
    BtnLabel.Gravity= Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.TOP)
   
    BkgPanel.AddView(BtnLabel,5dip,10dip + PicSize,btnWidth-10dip,LblH)
   
    Return BkgPanel
   
End Sub

In the main activity I call the class as follows:

B4X:
Sub Globals

    Private Panel1 As Panel 'main Layout
    Private Button1 As Button 'main Layout
   
    Private pnlButton As Panel
    Dim classBtn As CLbutton
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("main")
   
    classBtn.Initialize("clbClick",Me,150dip,150dip)
   
    pnlButton = classBtn.AsPanel
    Panel1.AddView(pnlButton,0,0,150dip,150dip)
   
   
End Sub

Unfortunately the call to classBtn.AsPanel returns an error :
java.lang.NullPointerException: Attempt to write to field 'int anywheresoftware.b4a.BALayout$LayoutParams.left' on a null object reference


Could someone please show me how to fix this problem, sample solution is attached, thanks.
 

Attachments

  • ClassButtonTest.zip
    8.5 KB · Views: 125

jvetterli

Member
Licensed User
Longtime User
Thanks for the quick reply and the hint to the User's Guide, should have looked there in the beginning as the explanations and examples are really helpful!
 
Upvote 0
Top