Running a Timer

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to start a Timer from a service module but having some issue in doing it.

In my Main Activity I have the following code:

B4X:
Sub Process_Globals
Dim connectiontimer As Timer         
   connectiontimer.Initialize("connectiontimer",3000)
   connectiontimer.Enabled = False
End Sub

Sub connectiontimer_tick
   Msgbox("test","")
   connectiontimer.Enabled = False
End Sub

In my Service module I am activating the Timer by doing the following (this gets activated by a event in the Service Module):

B4X:
Main.connectiontimer.Enabled = True
Main.connectiontimer.Interval = 3000

However when I run the above code on my device I get a message on my device saying is has stopped and closes.

In the B4A Log it comes up with the following error:
B4X:
java.lang.NullPointerException
   at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:104)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:213)
   at android.app.ActivityThread.main(ActivityThread.java:4788)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
   at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

Anyone know why this is happening or what I might be doing wrong?
 

aaronk

Well-Known Member
Licensed User
Longtime User
You shouldn't access a timer of a different module. You should instead add a timer to your service or use CallSubDelayed to start the timer from the activity code.

I get the same result when running the timer in the Service Module.

Service Module: Test
B4X:
Sub Process_Globals
   Dim connectiontimer As Timer         
      connectiontimer.Initialize("connectiontimer",3000)
      connectiontimer.Enabled = False   
End Sub

Then in a sub in my Service Module (Test) I run the following code:
B4X:
connectiontimer.Enabled = True
connectiontimer.Interval = 3000

Then I get the same error message.

I then tried CallSubDelayed like:

Main Module:
B4X:
Sub Process_Globals
   Dim connectiontimer As Timer         
      connectiontimer.Initialize("connectiontimer",3000)
      connectiontimer.Enabled = False
End Sub

Sub run_connection_timer
   connectiontimer.Interval = 3000
   connectiontimer.Enabled = True
End Sub

Sub connectiontimer_tick
   Msgbox("test","")
End Sub

Then from my Service Module I used the following code:
B4X:
CallSubDelayed(Main, "run_connection_timer")

But still get the same error message.
Based on my code it runs run_connection_timer but then gives an error when the connectiontimer_tick sub runs.
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I ended up getting it working by doing the following:

Main Module:
B4X:
Sub Process_Globals
    Dim connectiontimer As Timer            
End Sub

Sub run_connection_timer
    connectiontimer.Initialize("connectiontimer",3000)
    connectiontimer.Enabled = True
End Sub

Sub connectiontimer_tick
    Msgbox("test","")
End Sub

Service Module:
B4X:
CallSubDelayed(Main, "run_connection_timer")
 
Upvote 0
Top