Android Question Timer not Work.....

js1234

Member
Licensed User
Longtime User
Why Timer NOT work?

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

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim    Timer1    As    Timer
    Dim    iStejem As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout1")
    If FirstTime Then
        Log("Set timer.....")
        Timer1.Initialize("Timer1_Tick", 2000)
        Timer1.Enabled=True
        If Timer1.IsInitialized Then Log("Timer1 is INIT OK") Else Log("Timer1 is NOT init OK")
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub    Timer1_Tick
    Log("iStejem=" & iStejem)
    iStejem=iStejem + 1
    Timer1.Enabled=False
    Timer1.Interval=2000
    Timer1.Enabled=True
End Sub

Logger connected to: samsung SM-N910C
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Set timer.....
Timer1 is INIT OK
** Activity (main) Resume **
java.lang.Exception: Sub timer1_tick_tick was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:202)
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Set timer.....
Timer1 is INIT OK
** Activity (main) Resume **
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Why Timer NOT work?

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

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim    Timer1    As    Timer
    Dim    iStejem As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout1")
    If FirstTime Then
        Log("Set timer.....")
        Timer1.Initialize("Timer1_Tick", 2000)
        Timer1.Enabled=True
        If Timer1.IsInitialized Then Log("Timer1 is INIT OK") Else Log("Timer1 is NOT init OK")
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub    Timer1_Tick
    Log("iStejem=" & iStejem)
    iStejem=iStejem + 1
    Timer1.Enabled=False
    Timer1.Interval=2000
    Timer1.Enabled=True
End Sub

Your init is wrong. It should be:

B4X:
Timer1.Initialize("Timer1", 2000)

- Colin.
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Log("Set timer.....")
        Timer1.Initialize("Timer1", 2000)
        Timer1.Enabled=True
    End If
End Sub

Sub Timer1_Tick
    Log("iStejem=" & iStejem)
    iStejem=iStejem + 1
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Further explanation: When you initialize an object that takes an EventName parameter, you need to provide a "tag" for the events - but not the events themselves. The EventName parameter links the object's events to the object itself.

So for example, a Label has 2 events (Click & LongClick) - so obviously you can't initialize it with Label.Initialize("Label_Click") or Label.Initialize("Label_LongClick"), because then you would only be able to use one or the other event. When you initialize the Label, you use Label.Initialize("whatever"), then when you set up the event subs you will have:

B4X:
Private Sub whatever_Click
...
End Sub

Private Sub whatever_LongClick
...
End Sub

So the value you provide in the initialization is the reference that B4A uses to make sure events are tied to the correct object.

- Colin.
 
Upvote 0
Top