Android Question Application_Error not fired.

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Running v5.50 release. I renamed an existing service module to be "Starter". Change the file name on-disk. Open B4A, it complains it can't find old module. I add existing, the renamed file. It loads... all this is fine.

I added to service attributes
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

In starter I have....
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
         ETC....
End Sub

Sub Service_Create
    Log("at Starter.Service_Create")
           ETC....
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("at Starter.Service_Start")
    logcat.LogCatStart(Array As String(),"logcat")
        'lower cased module name
    NativeMe.InitializeStatic(Application.PackageName & ".starter")
End Sub

Sub Service_Destroy
    Log("at Starter.Service_Destroy")
            ETC.........
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Log("at Starter.Application_Error")
                  ETC....
    Return True       
End Sub

In the MAIN activity I have a button handler....

B4X:
Sub btnTest_Click
   Log("at Main.btnTest_Click")
   Dim i As Int
   i = "sdf"   
End Sub

ok, so then to test. I tried with debug build, and it did not invoke the error handler. In the fog I call my head, I recall something about needing to run a release build, but that also made no difference. In the logcat I can confirm the Application_Error was not called (no logged message). But I do see a pop-up from Android saying the app has stopped. Here is what I see in logcat...

PackageAdded: package:eek:dp.eljaydelivery
etc.
at Starter.Service_Create
etc.
at Starter.Service_Start
etc.
at Main.Activity_Create
etc.
at Main.Activity_Resume
etc.
at Starter.Service_Destroy
etc.
at Main.btnTest_Click
main_btntest_click (java line: 529)
java.lang.NumberFormatException: Invalid double: "sdf"
etc.
main_btntest_click (java line: 529)
java.lang.NumberFormatException: Invalid double: "sdf"
etc.

I don't ever see the log entry from Application_Error

Perhaps the way I renamed this service module to Starter, rather than creating one from scratch is at issue? I was under the impression the module name alone was all that was needed. Perhaps it doesn't catch the kind of exception I forced?
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I've used both, but I verified this morning that a release build running without either bridge or ADB/USB connection also apparently does not fire the event either.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
This small example illustrates the issue. I can run it from a release build, without the debugger. i.e. start from the tablet's app icon, and it does not catch the crash. At least I see no toast from the error handler. This behaves much as my main production project. Except in the production project, from time to time, I also see the Service_Destroy being called soon after the main activity starts. But I don't want to cloud the issue for this isolated question about getting Application_Error to fire.

I am testing on a Samsung Galaxy Note 2014 Edition 10.1" (SM-P600) It is running Android version 4.4.2
 

Attachments

  • StarterServiceTest.zip
    362.7 KB · Views: 191
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use File - Export as zip when uploading files.

The sub is called. It was a wrong suggestion to use a toast as the toast message is removed when the process is killed.

If you use USB debug mode then you will be able to see the logs when the app crashes.

You can test it with a notification instead:
B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo("App crashed", StackTrace, Main)
   n.Notify(1)
   Return True
End Sub
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I got my little test app, swapping Notify for Toast to work. So back to my big production app...

I'm new to Starter services. I used a service previously, but the main app controlled it's lifetime. I used it to tap into the LOGCAT stream.

What I observe is that the Application_Error is NOT fired. But I see that the Service_Destroyed event of the starter service is seen to occur early, prior to the induced error. I'm reasonably certain Application_Error will not execute without the starter service running.

I think this will be for me to work through here. But I've been banging my head for a few days trying to discover what causes my starter service to exit prematurely.

Sequence is like...
Starter.Service_Start
Main.Activity_Create
Main.Activity_Resume
Starter.Service_Destroy
httputils2service.Create
httputils2service.Start
httputils2service.Start
httputils2service.Start

I tried block commenting out the entire body of main.Activity_Resume, but the starter service is still being destroyed. Such a puzzle?!?!
 
Last edited:
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Ok... I see this now, you are right.

The thing tripping me up is attempting to manage shutdown of the starter service in all scenarios. Somewhere on forums I saw the advice to put the StopService in Main activity pause event, but that may kill it prematurely. I have several activities.

Another approach I tried is to tap into the cyclic activity of my starter service which collects logcat, and if all activities are paused, to consider it time to shut down the Starter service. I built a list of all modules, looking for any non-paused activity:
B4X:
Sub IsAnyActivityRunning As Boolean
   For Each module As Object In activities
     If IsPaused(module) = False Then Return True
   Next
   Return False
End Sub

But this does not produce a correct result either.

My starter service is 1) collecting logcat, and 2) providing an Application_Error handler.

There is yet another issue, but I will post to a separate thread.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
There is no harm in it continuing to run. But I want to cease logcat collection, because the main app that is no longer running. Trying to be a good citizen of the battery.
 
Upvote 0
Top