BTW, I tested your project with the new CodeBundle tool:
https://www.b4x.com/android/forum/t...a-single-json-for-ai-analysis.169835/#content
Then asked ChatGPT:
Short answer:
the button is on a different B4XPage than the code handling it, so the event is never wired.
What’s happening
- Button1 is defined in the imieiimpianti layout/page
- Button1_Click is written in B4XMainPage
- In B4XPages, events only fire in the page that loaded the layout
- You then call ShowPageAndRemovePreviousPages("Impianti"), so B4XMainPage is no longer active anyway
Result: Button1_Click in B4XMainPage will
never be raised.
I then asked it to "find other bugs":
The first point is moot, but the other are correct.
Here are
other real issues / likely bugs I see in your project (besides the Button1_Click page/layout mismatch):
1) B4XPagesManager is local in AppStart (can break the app)
In Main.AppStart you do:
- create Dim PagesManager As B4XPagesManager
- PagesManager.Initialize(MainForm)
- then AppStart ends
In the standard B4XPages template, PagesManager is
Process_Globals, not local, so it stays alive. With a local variable, you’re relying on internal references to keep it from being collected / lost.
Fix (recommended):
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private PagesManager As B4XPagesManager
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
PagesManager.Initialize(MainForm)
End Sub
2) You add the page before initializing the page object
In B4XMainPage.B4XPage_Created you do:
- B4XPages.AddPage("Impianti", pgImpianti)
- only later: pgImpianti.Initialize
If pgImpianti isn’t initialized yet, this is the wrong order (and can lead to subtle “page not working / events not firing / state missing” behavior).
Fix:
pgImpianti.Initialize
B4XPages.AddPage("Impianti", pgImpianti)
(Or use AddPageAndCreate if you want B4XPages to create it.)
3) B4XMainPage declares views that are not in the layout it loads
B4XMainPage loads "MainPage"
…but its globals include btAggiungi, Label4, Button1 and it even checks Label4.IsInitialized Those views are
in the other layout (imieiimpianti), which is loaded in the
other page (i_miei_impianti)
.
So in B4XMainPage, those fields + event subs are essentially
dead code and misleading.
Fix: Remove them from B4XMainPage, or move the relevant handlers into i_miei_impianti.
4) Sleep(1000) inside B4XPage_Created + “remove previous pages” is a foot-gun
You do:
- Sleep(1000)
- then ShowPageAndRemovePreviousPages("Impianti")
This can cause hard-to-debug timing issues (especially during startup / slow machines) and makes it easy to accidentally “remove” the page that contains the code you expect to run.
Fix idea: don’t use Sleep here; navigate immediately, or wait for a real condition (like “page created” / “data loaded”).
5) Duplicate input handlers on the same control (btAggiungi_Touch and _MouseClicked)
You have both:
- btAggiungi_Touch(...) and
- btAggiungi_MouseClicked(...)
On B4J you normally stick to mouse events; having both can lead to “double” messages or inconsistent behavior depending on control type and platform.
Fix: keep one (usually _MouseClicked for B4J desktop).