Android Question [SOLVED ]Cannot Access Activity Object from Sub Process Globals error

Peter Lewis

Active Member
Licensed User
Longtime User
Hi
I have a problem in a Android project where I am declaring the MQTT variable to be used in the app in Starter. I am getting this issue but I do not have ANY screen activity in that module. Below is the MQTT Module code. I find sometimes it works then a minute later it does not

Any ideas would be appreciated . Thank you

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
  
  
    Public mqttObj As MqttClient
    Private mqttUser As String = "" 'ignore
    Private mqttPassword As String = "" 'ignore
    Public working As Boolean = True
    Public done As Boolean = False
    Private mqttHost As String
    Private firstTime As Boolean
    Private subscribe As Boolean
    Private raf As B4XSerializator
    Private imageconvert As B4xconvertimage
    Private remote As B4xremotefunctions
    Private texts As B4xtextrelated
    Private tick As B4xtickrelated
    Private book As B4xBookingFunctions
    Private vehicle As B4xvehiclesfunctions
    Private admin As B4xadminfunctions
    Private attachments As B4xattachmentrelated
  
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    firstTime = True
    subscribe = True
    imageconvert.Initialize
    remote.Initialize
    texts.Initialize
    tick.Initialize
    book.Initialize
    vehicle.Initialize
    admin.Initialize
    attachments.Initialize
  
    Return Me
End Sub





public Sub ConnectAndReconnect(ClientId As String,Server As String, port As Int)
    Log(ClientId&" "&Server&" "&port)
    Do While working
        If mqttObj.IsInitialized Then mqttObj.Close
        Log("Trying to connect")
        mqttHost = "tcp://"&Server&":"&port
        mqttObj.Initialize("Client1", mqttHost, ClientId)
        Dim mo As MqttConnectOptions
        mo.Initialize("", "")
        'this message will be sent if the client is disconnected unexpectedly.
        mo.SetLastWill("all/disconnect", raf.ConvertObjectToBytes(Main.GlobalUserID), 0, False)
        mqttObj.Connect2(mo)
        Wait For Client1_Connected (Success As Boolean)
        If Success Then
            Log("Connected to the Server")
            If Main.newuser <> True Then
              
            End If
            mqttObj.Unsubscribe("#")
            mqttObj.Subscribe("clients/"&Main.tempuserID,2)
            mqttObj.Subscribe("clients/"&Main.GlobalUserID,2)
            mqttObj.Subscribe("clients/"&Main.origionator,2)
            mqttObj.Subscribe("delayed/"&Main.GlobalUserID,2)
            mqttObj.Subscribe("messagestatus/"&Main.GlobalUserID,2)
            mqttObj.Subscribe("users/status/"&Main.GlobalUserID,2)
            If Main.newuser=False Then
                B4XPages.MainPage.sendregname
            End If
            Do While working And mqttObj.Connected
                If subscribe Then
                    subscribe = False
                End If
                If firstTime Then
                    firstTime = False
                End If
                Sleep(3000)
            Loop
            Log("Disconnected")
            subscribe = True
        Else
            subscribe=True
            Log("Error connecting.")
        End If
        Sleep(2000)
    Loop
End Sub


Public Sub Client1_MessageArrived (Topic As String, Payload() As Byte)
    If Main.offline=False Then
        Log("message arrived "&Topic)
        If Main.su.ContainsSeq(Topic,"delayed") = False Then
            Log("undelayed")
            Dim raf As B4XSerializator
            Dim m As Map = raf.ConvertBytesToObject(Payload)
                  
            If m.ContainsKey("updatesubject") Then
                Main.sql1.ExecNonQuery2("UPDATE chat SET reference = ? WHERE messagedts = ?",Array As String(m.Get("updatesubject"),m.Get("messagedts")))
            End If

            If m.ContainsKey("profilepicdata") Then
                Dim c() As Byte =m.Get("profilepicdata")
                imageconvert.BytesToFile(Main.profilefolder,m.Get("filename"),c)
            End If
            If m.ContainsKey("messagedelivered") Then
                Log("message delivered text mqtt")
                tick.messagedelivered(m)
            End If
            If m.ContainsKey("messageviewed") Then
                Log("messageviewed text mqtt ")
                tick.messageviewed(m)
            End If

            If m.ContainsKey("messagedeliveredpic") Then
                Log("message delivered pic mqtt")
                tick.messagedeliveredpic(m)
            End If

            If m.ContainsKey("messageviewedpic") Then
                Log("messageviewed pic mqtt")
                tick.messageviewedpic(m)
            End If
              
            If m.ContainsKey("botdata") Then
                Dim mess As List = m.Get("botdata")
                admin.botinforeceived(mess)
            End If

            If m.ContainsKey("polldata") Then
                Dim mess As List = m.Get("polldata")
                admin.Pollusers(mess)
            End If

            If m.ContainsKey("vehicledata") Then
                Dim mess As List = m.Get("vehicledata")
                vehicle.vehicleinforeceived(mess)
            End If

            If m.ContainsKey("bookingdata") Then
                Dim mess As List = m.Get("bookingdata")
                book.bookinginforeceived(mess)
            End If

            If m.ContainsKey("chatname") Then
                Dim chatnames As List = m.Get("chatname")
                Dim servicenames As List = m.Get("servicenames")
                B4XPages.MainPage.fillchatnames(chatnames, servicenames)
            End If

            If m.ContainsKey("availdays") Then
                Dim mess As List = m.Get("availdays")
                book.fillcalendar(mess,m)
            End If

            If m.ContainsKey("activebookings") Then
                book.checkactivebooking(m)
            End If

            If m.ContainsKey("textmessage") Then
                texts.incomingmessage(m)
            End If


            If m.ContainsKey("vehdata") Then
                Log("subject data rec mqtt")
                Dim vehdata As List = m.Get("vehdata")
                B4XPages.MainPage.fillvehicles(vehdata)
            End If
              
            If m.ContainsKey("areadata") Then
              
                Dim areadata As List = m.Get("areadata")
                vehicle.scanvehicle(areadata)
            End If
          
        If m.ContainsKey("picture") Then
            attachments.incommingpictures(m)
            End If

                  
            If m.ContainsKey("pdf") Then
                attachments.incommingpdf(m)
            End If
                  
      

        End If
    End If
  
End Sub



Private Sub Client1_Disconnected
    mqttObj.Close
    'g.debugLog("Disconnected from the MQTT Broker")
End Sub

Public Sub SafeKill(pFolder As String, pName As String)
    Try
        File.Delete(pFolder,pName)
    Catch
    End Try'ignore
End Sub

Public Sub MQTTClose
    mqttObj.Close
End Sub
 

Peter Lewis

Active Member
Licensed User
Longtime User
As the error sayrs, you have an Activity object in Process_Globals somewhere. I don't think it is in the code you posted.
That is what I thought but I cannot find it. Is there anyway it is HIDDEN ?
1613135988627.png


This is the only other Process Globals in Main

1613136176664.png
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I had changed it back to a standard Class and still the same issue. I though that might have been the problem and called it clsMqtt, but did not help

1613138353041.png
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I don't understand. Is it fixed or not?
No It still does not work, i had tried that before as one of my debug processes, I have made sure that all the classes do not have
Private Root As B4XView 'ignore and also the Private Sub B4XPage_Created (Parent As B4XView) have been removed

but still the same issue
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
What line does the error point to when you click it? It should be in one of the Sub Process_Globals and if it is a class instance that class will have an Activity object in its Class_Globals.
This is the error in the logs

1613140536357.png


when I click on this it takes me to Starter and the MQTT assignment

and the ONLY one that is showing an error on any of the modules is the starter module as shown in Post 3 which is the MQTT.

There is only the MAIN and B4XMainPage which are Activity modules. The rest are classes. All the classes were initially in MainPage but I separated them to make it more manageable. Some of the classes do have a B4XView in their own Class Globals as the classes compile a Panel / Pane and send them back to the mainpage into a custom list view for display.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I assume you mean this line - why can't you just post it as I asked? We need to be very precise to solve problems and avoid making any assumptions whatsoever.
B4X:
 Public mqtt As B4xMqtt
You must still have an Activity object declared in that class.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
ok thank you for your help I appreciate it

In Post 6 there was a line which I did not know was an activity item. which solved the problem. Looks like sometimes we all slip up
 
Last edited:
Upvote 0
Top