Android Question Sleep(0) and Msgbox2Async is crashing

divinglog

Member
Licensed User
Longtime User
Hello

When I use Sleep together with async messagebox (in a class module) the code is crashing. Removing Sleep(0) from the loop works and the messagebox is shown, but the progressdialog is not shown. Is there any way to use both together?

B4X:
Public Sub Import(ImportFile As String) As ResumableSub
    ProgressDialogShow2("Importing...", False)
 
    For j = 0 To ImportTable.RowCount - 1
        'Prepare source data code
        Sleep(0)
    Next
 
    ProgressDialogHide
    Msgbox2Async("Do you want to import %1 rows?", "Import", "Yes", "", "No", Null, True) '< Crash when using Sleep(0) in the loop
    Wait For Msgbox_Result (MsgResult As Int)
 
    If MsgResult = DialogResponse.POSITIVE Then
        '...
    End If
End Sub


Crash Log:

B4X:
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to read from field 'java.lang.ref.WeakReference anywheresoftware.b4a.BA$SharedProcessBA.activityBA' on a null object reference
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1706)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.ref.WeakReference anywheresoftware.b4a.BA$SharedProcessBA.activityBA' on a null object reference
    at anywheresoftware.b4a.keywords.Common.Msgbox2Async(Common.java:486)
    at com.divinglog.divelog.import$ResumableSub_Import.resume(import.java:361)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1704)
    ... 7 more
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
The sub return as soon as you use sleep or wait for.
make the datafilling a resumeablesub.

 
Upvote 0

divinglog

Member
Licensed User
Longtime User
I'm not sure where this error comes from. Can you reproduce it in a small project and upload it?

No problem, I've attached a small project. Thank you Erel!

Edit: OK, I found out that the code is working directly in the Activity module. If it is in a class module, it crashes. So I think the activity reference is lost because of the Sleep() function and the messagebox cannot be displayed.
 

Attachments

  • AsyncTest.zip
    9.8 KB · Views: 205
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
It is related to the class not having an activity context.

It will work with this code:
B4X:
Sub Class_Globals
    Private stub As Button 'ignore
End Sub
Will a permanent fix for this be in the next version of B4A so this line doesn't need to be added?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Is this an issue that is related to this bug?

 
Upvote 0
Top