It's a commercial app I am working on so I can't upload it. However, when preparing a test project for you to see, I actually found the cause.
In the app (XUI) I have delegated must of the work to a separate module that loads the layout etc. However, the auto generated stub and Dim statement was created in the main activity, not the module. That's why it was uninitialized.
I moved the Dim and event handler to the correct module and then it worked.