Android Code Snippet Email an error report

Sub Name: ReportError

Description: Have your app email an error report to you.

This gets asked for often in the chat room. It sends an error report to you with the following information from your app:
App name
App version
Phone make and model
Full java stack trace

Include this in your app or in a data module. Surround important areas that you want reports from with a Try Catch statement. Call the ReportError sub in the Catch clause so it will only be executed on an error. That's it!

You can call this from as many subs as you like. Anywhere that you want to be notified of an error. To use the stack trace that is generated, go to your project directory and look in the Objects\src folder. You will see more folders that are the same as your project name such as:

Objects\src\org\mlsoft\VirtualDyno

Then you'll see various java files there. The one you want is main.java. Open that file and go to the line number reported in the error report. Above it, you will see a b4a reference line number. That is the line number to look at in the b4a IDE to find the error.

You will need to add the following libraries to your project:

Phone library
Threading library
StringUtils library

pname is the name of the app
ver is the version of the app
inetAddress is your email address (where you want the report to go)

B4X:
Sub ReportError(pName As String, Ver As String, inetAddress As String)
    Dim ex As ExceptionEx 'from the Threading library
    Dim error,ptype As String 'the error string and phone type info
    Dim mail As Email 'built in email type
 
    ex.Initialize(LastException.Message)
    ptype = ph.Manufacturer & " " & ph.Model & CRLF & pName & " version " & Ver & CRLF
    error = ptype & CRLF & ex.ToString & CRLF & CRLF & ex.StackTrace & CRLF
 
    If Msgbox2("An error has occurred. The error is : "&LastException.Message&". Please report this error below.","ERROR","Report","","Dismiss",Null) = DialogResponse.POSITIVE Then
    mail.Attachments.Clear
 
    mail.Subject= pName & " error" 'Example: Virtual Dyno error
    mail.To.Add(inetAddress)
     
    mail.Body= error
 
    StartActivity(mail.GetIntent) 'calls the intent to show the email client
    End If
 
End Sub

Tags email,error,report
 

HotShoe

Well-Known Member
Licensed User
Longtime User
This DOES open the email client. There are ways to send things without the users knowledge or interaction, but I like the user to know what is happening. It only requires the user to tap the Send button after he is sure he/she is OK with the report. I find this is better than users not trusting my software.

--- Jem
 

grafsoft

Well-Known Member
Licensed User
Longtime User
Thank you!! I had a similar routine, but yours is much better.

What I do additionally is writing a protocol, like this:

B4X:
Sub GPS_locationchanged (Location1 As Location)
   main.prot.add ("GPS_locationchanged")
   Try
     lati=Location1.latitude
     longi=Location1.Longitude
     Btngps.Visible=False
   Catch
     Dim s As String
     s=LastException.Message
     Log (s)
     Main.prot.Add (s)
   End Try
End Sub

Then I add the contents of the list main.prot to the body of the mail. Looks like an overkill, but I can add additional information to this protocol to further analyze the history of the error. Sometimes you need to know whar actions were taken and in which order.
 
Top