Android Question how to debug this? Sleep(0) vs DoEvents

peacemaker

Expert
Licensed User
Longtime User
HI, All

App user informed me that the latest version is stopped after some time.
After some test i have found that after 360 seconds ALWAYS app is stopped with such error, even in the Debug mode:


Starter.tim_Tick: 357
Starter.tim_Tick: 358
Starter.tim_Tick: 360
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4a.BA.isActivityPaused()' on a null object reference
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:104)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

It seems to me that it is some timer tick sub and isPaused function. But i cannot find such function usage in all timers subs.
App is old, and i found that DOEVENTS is still used, but not all places, some are already with Sleep(0) instead.

And when i placed DOEVENTS everywere instead of Sleep(0) .... hanging is over, app works fully OK.
When to use Sleep(0) - it's again error.

How to understand it ?
 

agraham

Expert
Licensed User
Longtime User
The error is occurring when the Timer is trying to invoke your Timer Sub. It seems to be finding that the ba object is null.
Java:
    public void run() {
      Timer parentTimer = this.parent.timer;
      if (parentTimer == null || this.currentTimer != parentTimer.relevantTimer)
        return;
      BA.handler.postDelayed(this, parentTimer.interval);
      if (!this.ba.isActivityPaused() && !Msgbox.msgboxIsVisible()) // <--------------This is line 104
        this.ba.raiseEvent2(parentTimer, false, parentTimer.eventName, true, new Object[0]);
    }
  }
Is your Timer in the Starter service, it looks like it, or in another Service? As it is an old app it looks like some behaviour in Android has changed in later versions.

DoEvents and Sleep behave differently. DoEvents executes some of the messages on the app message loop without returning to the message loop then continues in your Sub. Sleep actually returns to the app message loop and then your Sub is re-entered some time later. Because DoEvents executes the messages itself it has become very fragile over the years as the message loop code has become more complicated and is now deprecated.
 
Upvote 1

peacemaker

Expert
Licensed User
Longtime User
or in another Service
Yes, timer in a separate foreground service with a notification. All the time it was tested under Android 12.
How to be here ?
 
Last edited:
Upvote 0
Top