Android Question GetActivityBA causing NullPointerException

swabygw

Active Member
Licensed User
Longtime User
I have the following code in a code module:

B4X:
Sub getCurrentActivity As Activity
   Dim r As Reflector
   r.Target = r.GetActivityBA  'this line causes NullPointerException
   Return r.GetField("vg")
End Sub

I call it from the Starter (Service) module within a Tick event for a timer, but get the NullPointerException error on the line indicated above. I tried GetActivity and got a similar error (and, from what I understand, GetActivityBA is correct). Is the error caused because I'm calling it from the Starter module?

P.S., I have the Reflection library included.
 

swabygw

Active Member
Licensed User
Longtime User
Ah - that makes sense. My idea was to have a Timer (and the Tick event) created in the Service module so it would keep running regardless of which Activity the user is in. The Tick event makes a change to a view in whichever Activity the user is looking at at the moment of the Tick...but it needs to know which Activity is active at the moment. So, I thought that the function above would help identify which Activity is active at the moment, and thought it could run anywhere so I put it in the Code module thinking that it would be most reusable there. But now I think I'll just put a code snippet in each activity. Thanks, again.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
FYI - I found a simple workaround: from the Tick event in the Service module, just call the Sub in each of the Activity's and the one that is open will fire, like this -

B4X:
Sub MyTimer_Tick()
  If Not (IsPaused(Main)) Then CallSub(Main, "MainTickEvent")
  If Not (IsPaused(Act2)) Then CallSub(Act2, "Act2TickEvent")
  If Not (IsPaused(Act3)) Then CallSub(Act3, "Act3TickEvent")
  If Not (IsPaused(Act4)) Then CallSub(Act4, "Act4TickEvent")
End Sub

For the closed Activity's, I thought that CallSub would either cause an error or open up the Activity, but it does neither. So, this worked out perfectly.
 
Upvote 0
Top