Android Question Returning from a Wait For function and getting BadTokenException error

colboy

Member
Licensed User
I am creating a form and then calling a function and getting the system to wait. I'm calling from the form and not a service like this:

B4X:
        wait for (GlobalFunctions.ParseCycleCount(lJson,lSAPData)) Complete (Result As Boolean)
   If Result=True Then
      btnOK.enabled=True
      btnCancel.Enabled=True
      Msgbox2("Local Data Successfully Updated","Info","OK","","",Null)
      Return
   Else
      Return
   End If
The skeleton for the function is contained in my GlobalFunctions file and looks like this:

B4X:
Sub ParseCycleCount(pFile As String, pSAP As String) As ResumableSub
    Dim lResult As Boolean =False

   ... Do Stuff
   Return True 'If it works, otherwise False
End Sub
The function runs fine, but when it comes back to the calling module, I get the following error:

B4X:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@136049c is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:920)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:105)
    at android.widget.Toast$TN.handleShow(Toast.java:747)
    at android.widget.Toast$TN$2.handleMessage(Toast.java:622)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
I don't think I'm doing anything unusual. Does anyone have any experience with this?

Colin
 

colboy

Member
Licensed User
In the ParseCyclecount function there numerous blocks of code to import data into SQLite. The last of these imports 55,000 records. If I disable this, everything works fine. I then tried to disable all of the ToastMessages being created, and again this prevents the error, even with the 55,000 record import.
 

Erel

Administrator
Staff member
Licensed User
First step is to remove the Msgbox call. Only show async dialogs.
 
Top