Android Question Addview error in class

walt61

Well-Known Member
Licensed User
Longtime User
Hi all,

I'm getting the following error when trying to add a panel to a ScrollView in a Class (and obviously don't understand what I'm doing wrong):

java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
at anywheresoftware.b4a.objects.ViewWrapper.getHeight(ViewWrapper.java:143)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

In Main, Ativity_Create does this:
B4X:
Dim p As myclass
p.Initialize(Activity, "My class")
p.Show

With 'myclass' I want to add a ScrollView to an Activity and then fill that Scrollview. The class module looks like:
B4X:
Sub Class_Globals

   Private dummyViewToSetActivityContext As View ' Ignore - see https://www.b4x.com/android/forum/threads/an-activity-context-is-required-error.38903/

   Private myTitleText As String
   Private myActivity As Activity
   Private mySV As ScrollView
   Private mySVassembled As Boolean = False

End Sub

Public Sub Initialize(act As Activity, title As String)

   If mySV.IsInitialized Then Return

   myActivity = act
   myTitleText = title

   mySV.Initialize(myActivity.Height)
   myActivity.AddView(mySV, 0, 0, myActivity.Width, myActivity.Height)
   mySV.Panel.Width = myActivity.Width
   mySV.Panel.Height = 0
   mySV.Panel.Color = Colors.LightGray

End Sub

Public Sub Show ' FYI: this is not done in 'Initialize' because more properties would be set between 'Initialize' and 'Show'

   If mySVassembled = False Then AssembleMySV
   mySV.Visible = True

End Sub

Private Sub AssembleMySV()

   Dim su As StringUtils

   mySVassembled = True

   Dim titlePnl As Panel
   titlePnl.Initialize("")
   titlePnl.Color = Colors.Black

   Dim titleLbl As Label
   titleLbl.Initialize("")
   titlePnl.AddView(titleLbl, 0, 2dip, mySV.Panel.Width, 20dip)
   titleLbl.Text = myTitleText
   titleLbl.TextColor = Colors.White
   titleLbl.TextSize = 15
   titleLbl.Height = su.MeasureMultilineTextHeight(titleLbl, titleLbl.Text)
   titleLbl.Gravity = Gravity.CENTER

   ' THE ERROR OCCURS IN THIS NEXT STATEMENT:
   mySV.Panel.AddView(titlePnl, 0, 0, mySV.Panel.Width, titlePnl.Height)

   titlePnl.Height = titleLbl.Height + 2dip

   ' ... more would be added here, of course ...

End Sub

I must be missing/misunderstanding something fundamental here; any pointers would be greatly appreciated,

W
 
Top