Android Question Astreams_NewData() in a service cause null receiver?

william_chen

Member
Licensed User
Hello guys,

In my app, I created a dedicated service to handle TCP communication. And I used AsyncStreams and initialize(with prefix), send and receive data all inside this service.
Everything seems to work well in release mode. However, in Debug mode, the app always crash at the "End Sub" line of following sub, with error message "null receiver" (or sometimes: expected xxx_activity_name receiver but got null):

B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg, False)
End Sub
Strange thing is that: if I set a breakpoint at this crashing line or somewhere inside the sub, and then the app will not crash and continue working.

My guess is that, AsyncStreams uses two separate threads to handle sending and receiving data. There is no problem with sending because the calling thread is there. However, when data are received, the NewData event is raised and AStreams_NewData() sub is called. Now, system is confused where should the execution goes after finishing this sub.

How can I solve this problems?

Thank you very much.

Best Regards,

William
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Can you post the full error message from the logs (right click to copy)?
 

tigrot

Well-Known Member
Licensed User
I happened many times to get errors using toastmessages in services. Use log() to show while debugging... maybe services are not UI interface friendly? In this case I delegate the display to an activity, using callsubdelay
 
Top