Panel.Width NullPointerException

barx

Well-Known Member
Licensed User
Longtime User
OK I may be missing something here, so need a second pair of eyes to look this over. I am using a tabhostview and adding panels holding other views rather than loading layouts. In the code below I get a NullPointerException on the line

B4X:
   pnlResults.Width = TabMain.Width - 20dip

I have been running this with just the pnlConductors operational and it works fine. I have just started setting up the pnlResults bit and got this error. I cannot understand it as the setting up code is practically identical to the pnlConductors code. Hope that makes sense, anyways here is the code.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("TabScreen")
   Activity.Title = "Conduit Size Calculator"
   
   #Region Initialize Views
   'Lists
   Conductors.Initialize
   
   'Maps
   ConductorCount.Initialize
   
   'Panels
   pnlConductors.Initialize("")
   pnlResults.Initialize("")
   
   'Labels
   
   
   'Buttons
   
   
   'Webviews
   wvResults.Initialize("") 'No event name currently required
   
   #End Region
   TabMain.Width = 100%x
   TabMain.Height = 100%y
   TabMain.AddTab2("Conductors", pnlConductors)
   TabMain.AddTab2("Results", pnlResults)
 
   
   #Region pnlConductors
   pnlConductors.Width = TabMain.Width - 20dip
   pnlConductors.Height = TabMain.Height
   pnlConductors.LoadLayout("ConduitConductors")
   svConductors.Color = Colors.Black
   svConductors.Panel.Color = Colors.Black
   svConductors.Panel.Width = svConductors.Width   
      
   spinLength.AddAll(Array As String("1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5", "6", "7", "8", "9", "10"))
   spinBends.AddAll(Array As String("0", "1", "2", "3", "4"))
   
   
   #End Region
   
   #Region pnlResults
   pnlResults.Width = TabMain.Width - 20dip
   pnlResults.Height = TabMain.Height
   pnlResults.AddView(wvResults, 0, 0, pnlResults.Width, pnlResults.Height)
   wvResults.Color = Colors.Black
   wvResults.ZoomEnabled = False
   #End Region
   
   LoadResults
   LoadConductors
End Sub
 

barx

Well-Known Member
Licensed User
Longtime User
You have to add the view to the activity first before getting the width or height.

The tabhost is added in activity.loadlayout and the pnlResults is added to the tabhost. Same goes for pnlConductors.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Unfiltered log:-

B4X:
calcconduitsize_activity_create (B4A line: 73)


pnlResults.Width = TabMain.Width - 20dip

java.lang.NullPointerException
   at anywheresoftware.b4a.objects.ViewWrapper.setWidth(ViewWrapper.java:112)
   at barkernet.electriciansreference.calcconduitsize._activity_create(calcconduitsize.java:318)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
   at barkernet.electriciansreference.calcconduitsize.afterFirstLayout(calcconduitsize.java:84)
   at barkernet.electriciansreference.calcconduitsize.access$100(calcconduitsize.java:16)
   at barkernet.electriciansreference.calcconduitsize$WaitForLayout.run(calcconduitsize.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3835)
   at java.lang.reflect.Method.invokeNative(Native Method)


   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
GC_CONCURRENT freed 391K, 49% free 3210K/6279K, external 1065K/1254K, paused 2ms+5ms


GC_EXPLICIT freed 94K, 47% free 3558K/6663K, external 0K/0K, paused 68ms


acquireWifiLockLocked: WifiLock{NetworkLocationLocator type=2 binder=android.os.BinderProxy@40b965e0}


releaseWifiLockLocked: WifiLock{NetworkLocationLocator type=2 binder=android.os.BinderProxy@40b965e0}
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I ran into this a lot lately with my class I've been working forever on. Seemed to always run me in circles because I needed the width/height to add the view, but needed to calculate the width/height using or setting the width/height to get the calculation...and the cycle continued. That and the stupid -1 widths panels have inside scrollviews/Tabs...there is a function to get the real width anyway, but it uses DoEvents to let the Activity think Create is finished and render the Panel to get the true width which caused the drawing to look odd.
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Well, this gets a little more interesting.

I changed the order in which the tabs are added. i.e.

B4X:
TabMain.AddTab2("Results", pnlResults)
TabMain.AddTab2("Conductors", pnlConductors

And now the error moves to the pnlConductors equivalent
B4X:
   pnlConductors.Width = TabMain.Width - 20dip

So it would appear that the problem lies with whatever tab is added second (And possibly any after that, but i only need 2)

As mentioned above, it's as though the panel doesn't realize it has been added to the tabhost.

any takers for the next move ???
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I actually do something weird on my tabs and use one single panel. On Tab Changes I just change the views on the Panel (In one app this is really easy since all tabs look the same and my Listview content just changes so I only edit that). Sounds like when you add panels to a Tab it is only setting up the first one anyway and causing problems.
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
As I understand, the idea is to change one panel at time, but It doesn't work to me.
I understand wrong?
Somebody can explain it better or with another idea?

Thanks a lot
 
Upvote 0
Top