Android Question Unexpected event (missing RaiseSynchronousEvents)

agraham

Expert
Licensed User
Longtime User
Something has changed over the years :(

I am revisiting my Basic scripting interpreter that worked fine back in 2012. I have changed nothing in the B4A app or the library but it no longer runs.

Now when an event is raised in my BasicIDE library I get this exception in the Logs

Unexpected event (missing RaiseSynchronousEvents): setvisible
Check the unfiltered logs for the full stack trace.

CallHostSub : setvisible returned Null at line 219


SetVisible is the B4A Sub being called from the library but this happens whichever Sub being called as an event.

B4X:
Sub SetVisible(name As String, visible As String)
   Dim v As View
   v = viewmap.Get(name)
   If v.IsInitialized Then
       v.visible = visible
   End If
End Sub


The code raising the event in the library is as follows. This used to work but now doesn't so I guess the event mechanism has changed in the last six years.

It looks like raiseEvent2 is returning null but this event is not being raised during Activity_Create but during normal program operation
B4X:
    private String callhostsub(String sub, Object[] args, boolean runonguithread)
   {
       try
       {
           sub = sub.toLowerCase();
           if (args == null)
               args = new String[0];
           if (!ba.subExists(sub))
           {
               saveerror("CallHostSub :  Sub " + sub + " not found!");
               return null;
           }
           // we need to check the thread we are on at event or call time
           // because we could be newed on main thread and invoked on another
           if (!runonguithread || (Thread.currentThread() == guithread))
           {
               String ret; 
               // use raiseEvent2 to let it run during Activity_Create
               ret = (String) ba.raiseEvent2(this, true, sub, false, args);
               // raiseEvent returns null during Activity_Create
               if (ret == null)
               {
                   // unlikely to get this now but left in just in case
                   String msg = "CallHostSub : " + sub + " returned Null";
                   saveerror(msg);
                   Log.e("B4A", msg + " at line " + linenum);
               }
               return ret;
           }
           TaskId += 1;
           ba.raiseEventFromDifferentThread(this, this, TaskId, sub, false, args);
       } catch (Exception e)
       {
           String msg = "CallHostSub : " + sub + " - " + e.toString() + " - " + e.getMessage();
           saveerror(msg);
           Log.e("B4A", msg + " at line " + linenum);
           Log.e("B4A", "", e); // stack trace         
       }
       return "";
   }
Any ideas on what I should change to make this work again?
 

agraham

Expert
Licensed User
Longtime User
I've a bit more information on this error. In fact some of the time the callhostsub(...) sub above is actually working. The problem seems to arise when it is called from within code that is already responding to an event from a view, such as a button click or checkbox changed event. The (complicated) sequence is that B4A view event code calls into the library to execute code there and that library code then calls callhostsub(...) to execute another B4A Sub to perform some UI function.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Actually callhostsub(...) works when called from code running in Activity_Create, which as the comment in the code shows, uses raiseEvent2 to let it work during Activity_Create (after 6 years the details escape me!). The problem arises once Activity_Create returns and the code starts reacting to events from views.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Perhaps this:
YESS! Thanks, for that. I thought that I was playing it safe by running it under the debugger to catch any errors in the log but in fact it was causing the problem. At first glance it seems to work fine in release mode so I'll play more tomorrow. I've just been called to dinner :)
 
Upvote 0
Top