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

Discussion in 'Android Questions' started by william_chen, Sep 8, 2016.

  1. william_chen

    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):

    Code:
    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: Sep 8, 2016
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full error message from the logs (right click to copy)?
     
  3. william_chen

    william_chen Member Licensed User

    Thank you, Erel.
    The problem has been solved by simply putting a callsubdelay() at the end of the above sub.
     
  4. tigrot

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice