Android Question java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String de.amberhome.obje

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Using the ACToolbar and all working fine, except I am unable to access the menu items outside
the Sub Activity_CreateMenu. I need to enable or disable certain overflow menu items, depending
on the active panel. When I try to do this in the Sub that handles the various panels:

B4X:
ActionBar.Menu.FindItem(9).Enabled = (iPanelType = ePanelType.SQLEdit)
ActionBar.Menu.FindItem(10).Enabled = (iPanelType = ePanelType.SQLEdit)
ActionBar.Menu.FindItem(11).Enabled = (iPanelType = ePanelType.SQLEdit)

I get a crash as mentioned.
I can see why this is as if I do:

Log(ActionBar.Menu.Size)
I get zero.

When I do the same at the end of Sub Activity_CreateMenu I get the expected number of 20.

This is the code creating the menu:

B4X:
#IgnoreWarnings: 11
Sub Activity_CreateMenu(Menu As ACMenu)
 
 Dim item As ACMenuItem

 Menu.Clear
 
 item = Menu.Add2(1, 1, " Find", TextToBitmapDrawable("F", 24, Colors.RGB(0, 0, 0)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 item = Menu.Add2(2, 2, " Pat", TextToBitmapDrawable("P", 24, Colors.RGB(0, 0, 255)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 item = Menu.Add2(3, 3, " Meds", TextToBitmapDrawable("M", 24, Colors.RGB(255, 0, 0)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 item = Menu.Add2(4, 4, " Vals", TextToBitmapDrawable("V", 24, Colors.RGB(0, 192, 0)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 item = Menu.Add2(5, 5, " Clin", TextToBitmapDrawable("C", 24, Colors.RGB(255, 0, 255)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 item = Menu.Add2(6, 6, " BP", TextToBitmapDrawable("B", 24, Colors.RGB(0, 255, 255)))
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 'overflow menu (3 dots)
 '----------------------
 item = Menu.Add(7, 7, " Settings", Null)
 item = Menu.Add(8, 8, " SQL", Null)
 item = Menu.Add(9, 9, " Clear SQL", Null)
 item = Menu.Add(10, 10, " Copy SQL", Null)
 item = Menu.Add(11, 11, " Paste SQL", Null)
 item = Menu.Add(12, 12, " Phone Patient", Null)
 item = Menu.Add(13, 13, " Text Patient", Null)
 
 'note overflow can't do submenu's
 '--------------------------------
 item = Menu.Add(14, 14, "-Add To Patient List", Null)
 item = Menu.Add(15, 15, "-Remove from Patient List", Null)
 item = Menu.Add(16, 16, "-Show Patient List", Null)
 item = Menu.Add(17, 17, "-Clear Patient List", Null)
 
 item = Menu.Add(18, 18," Make Chart", Null)
 item = Menu.Add(19, 19, " About", Null)
 item = Menu.Add(20, 20, " Exit", Null)
 
 'will be fine here, 20 menu items
 'General.RunLog("GotoPanel, menu items: " & ActionBar.Menu.Size)

End Sub

Any idea what I might be doing wrong?


RBS
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User

That is exactly the tutorial I read to get my current setup.
I compared the mentioned example with my setup and couldn't see the difference.
I do have the same Java code in the same place.
I add this to the example given:

B4X:
Sub btnTest_Click
 Log(ToolBar.Menu.FindItem(1).Title)
End Sub

And that works indeed, showing Overflow1.
Somehow, somewhere in my app the visibility of the menu items get lost.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
At least you got it sorted. ;-)

It took me a lot longer than it should, but learned something there, which is to spot the obvious problem:

> Log(ActionBar.Menu.Size)
> I get zero.

All this can mean is that the menu is not there yet, so my menu altering code runs too early.
I think I got led astray by the Java code:

B4X:
#If Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
 if (processBA.subExists("activity_createmenu")) {
  processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
  return true;
 }
 else
  return false;
}
#End If 
[CODE]

As I don't understand this code it must have caused some confusion.

RBS
 
Upvote 0
Top