Android Question Problem setting Label.Text

Rodrigo Muñoz

Member
Licensed User
Longtime User
Hello,

My program is crashing or locked somewhere, and there's no error reported in the IDE. The full code is below.

Using the IDE designer, I added a label called lblStory, whose text I want to change from another thread. In this new thread, called PlayWav, I change the label's text with a for loop. The for loop rarely reaches the end; it stops after a certain random number of iterations. I'm testing on Samsung Galaxy Win.

What could be happening?

B4X:
#Region  Project Attributes
    #ApplicationLabel: Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub
Sub Globals

Dim PlayThread As Thread
Private lblStory As Label

End Sub
Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("1")

lblStory.Text = "0"
lblStory.Invalidate

PlayThread.Initialise("PlayWav")
PlayThread.Start(Null, "PlayWav", Null)

End Sub
Sub PlayWav()

Dim i As Int
For i = 1 To 1000
  lblStory.Text = i
    'lblStory.Invalidate
    'PlayThread.sleep(10)
Next
Log("Made it")

End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Rodrigo Muñoz

Member
Licensed User
Longtime User
Thanks Erel,

I don't like threads and timers, and if I could I would process everything in a loop in the main thread (even UI events if it were posible!). I miss DOS :)

Anyway, I created the thread to process and play a wav sound file; as suggested in another post, it's better to soundstream in a secondary thread.

I want to raise some UI messages based on things that happen in that secondary thread, and that's why I wanted to change lblStory.Text from that thread. Now that you mention that I can't do that, I added a timer to pop that message. The secondary thread changes a global flag instead of lblStory.Text, and then the timer reads that flag and pops the message. It works now.

Still curious as why the secondary thread allowed me to change lblStory.Text a few times.

Rod
 
Upvote 0
Top