Android Question Do I need to initialize a button in the Button_Click sub?

TedDog

Member
Licensed User
Longtime User
One of my users told me that he is getting the following error:

An error has occurred in sub: java.lang.RuntimeException: Object should first be initialized.

I'm hunting for the cause, and have a question. In this sub

B4X:
Sub Button_Click
    Dim b As Button
    b = Sender

Do I need to do b.initialize? If so, what would be the EventName? I did initialize the sender in the Activity_Create sub.
 

stevel05

Expert
Licensed User
Longtime User
No you shouldn't initialize B, Sender should return an initialized object.

What do you mean by
I did initialize the sender in the Activity_Create sub.

If you added the button in the designer, then you shouldn't initialize it in Activity_Create.
 
Upvote 0

TedDog

Member
Licensed User
Longtime User
Thanks for looking at this, and helping me out. I didn't add it in the designer. I copied most of the code from a tutorial on this forum. The comments in the code are copied from the tutorial.

B4X:
' First I Dim'd an array of buttons
Sub Globals
Dim Buttons(4, 4) As Button

' Then I made a 3x4 grid of buttons
Sub Activity_Create
For y = 0 To 3
        For x = 0 To 2
            Dim b As Button
            b.Initialize("button") 'All buttons share the same event sub
'etc.

'Then I did this, which I don't fully understand
Buttons(x, y) = b 'store a reference to this view

'Then this, as noted in the first post
Sub Button_Click
    'Using Sender we find the button that raised this event
    Dim b As Button
    b = Sender

By the way, I'm not getting the error on my Nexus 4. The user who got the error said he's using Android version 4.4.2 on a Moto G 16GB.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That looks OK, but you don't really know where the error occurs as you don't have a sub name of line number.

Are you managing the apps lifecycle? Or does it completely rebuild itself on restart.

Do you get the error if the app goes into the background and back again.
 
Upvote 0

TedDog

Member
Licensed User
Longtime User
I didn't hear back from my user regarding which activity he was using when the error returned. Going through my code with a fine-toothed comb, I found a list and a period that I didn't initialize. Odd that they didn't cause an error on my device. Thanks again for your help.
 
Upvote 0
Top