Android Question MsgboxAsync Crash. Why?

Computersmith64

Well-Known Member
Licensed User
I have been replacing all the Msgbox & Msgbox2 calls in my apps with MsgboxAsync & Msgbox2Async calls. After reading Erel's tutorial, I came to the conclusion that for a MsgboxAsync call, there is not necessarily a requirement to use a Wait For Msgbox_Result if there isn't any code after the dialog that you want to hold off executing until the dialog has been dismissed.

So, with this in mind I implemented the following sub:
B4X:
Sub mnuAbout_Click
    Private sMsg As String
   
    sMsg = "myPets v" & Get_Version & CRLF & CRLF & _
            "Copyright (c) 2015-2017 Computersmith" & CRLF & CRLF & _
            "Icons created by Freepik (www.freepik.com)"
           
    MsgboxAsync(sMsg, "About myPets")
    
   
End Sub

This sub is called from an Activity menu item, so is run pretty much in isolation - therefore I figured that the Wait For would be not required - but in testing, I discovered that it will crash every time when the OK button is tapped. This is the error I get in the debug log:
An error occurred:
(Line: 1062) End Sub
java.lang.Exception: Sub msgbox_result signature does not match expected signature.
public static void com.airlinemates.mypets.main_subs_1._msgbox_result() throws java.lang.Exception
If I put in a Wait For, it no longer crashes.

Any ideas why?

- Colin.
 

DonManfred

Expert
Licensed User
How does you event sub signature (msgbox_result) look like?
If you dont use wait for then you need to have the event defined....
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
How does you event sub signature (msgbox_result) look like?
If you dont use wait for then you need to have the event defined....
Oh - ok, I must have missed that in the tutorial. I don't have it defined at all. Maybe that's the problem. What is the signature supposed to look like?

- Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
What is the signature supposed to look like?
Based on the code in the tutorial about async dialogs
B4X:
Msgbox2Async("Delete?", "Title", "Yes", "Cancel", "No", Null, False)
Wait For Msgbox_Result (Result As Int)
If Result = DialogResponse.POSITIVE Then
   '...
End If
i would guess
B4X:
sub Msgbox_Result (Result As Int)
end sub

But: why not use the wait for to react on the result in the same sub?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Based on the code in the tutorial about async dialogs
B4X:
Msgbox2Async("Delete?", "Title", "Yes", "Cancel", "No", Null, False)
Wait For Msgbox_Result (Result As Int)
If Result = DialogResponse.POSITIVE Then
   '...
End If
i would guess
B4X:
sub Msgbox_Result (Result As Int)
end sub
Yeah - thanks, I figured it out in the meantime.... :p

That seems to have resolved the issue. I guess I can just put a Wait For in too, but it seems redundant if it's the last line of code in the sub.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Based on the code in the tutorial about async dialogs
But: why not use the wait for to react on the result in the same sub?

That's the thing though - there's no reason to use a Wait For because there's nothing to react to & no code to execute after the user taps the OK button in the dialog. Therefore it seems redundant (& inefficient) to have the sub resume when there's nothing for it to do.

Or maybe I'm just not understanding the implementation correctly.... :)

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
You are correct. There is no reason to use Wait For here (the code example also don't include a Wait For).

Is this code inside a class?
No - it's inside my Main activity. So are you saying that I also don't need an empty implementation of the Msgbox_Result method?

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
It looks like a bug. Can you reproduce it in a small project?
Hi Erel,

I've attached a project that will crash - however I'm thinking that I might be causing the issue by having a MsgboxAsync with a Wait For Msgbox_Result in the Activity_Create sub. Is that allowed?

- Colin.
 

Attachments

  • msgboxcrash.zip
    7.7 KB · Views: 88
Upvote 0

Computersmith64

Well-Known Member
Licensed User
i would guess no.
It should be called after activity_resume is raised
I've always had Msgbox calls in Activity_Create in this particular app & they've never caused me issues (that I'm aware of) until I changed them to Async calls. In fact, even the Async call in Activity_Create isn't triggering the crash - it's the MsgboxAsync call in the menu_Click event that's doing it. Granted, it doesn't crash if the call in Activity_Create isn't there, but I'd like to understand why this causes the issue later on - because it only happens when I don't have either a Wait For or a Msgbox_Result declared for the later MsgboxAsync call.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
So this is interesting. Based on DonManfred's comments, I moved the first MsgboxAsync call out of Activity_Create & into its own sub that is called by a menu item. So now I have 2 menu items ("Show First Dialog" & "Show Second Dialog") & 2 separate subs linked to them that call MsgboxAsync. The first one uses a Wait For Msgbox_Result & the second one doesn't.

The interesting part is that regardless of whether or not I show the first dialog, the second dialog always crashes when I tap the OK button. So just having that first call in the activity code is enough to make any subsequent call without a Wait For crash.

This definitely seems like a bug doesn't it @Erel?

Project is attached.

- Colin.
 

Attachments

  • msgboxcrash2.zip
    7.4 KB · Views: 82
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Tip: use smart string literals for multiline text:
B4X:
sMsg = $"myPets v${Get_Version}
Copyright (c) 2015-2017 Computersmith
Icons created by Freepik (www.freepik.com)"$
Thanks - actually I have been using them for a while. I just haven't updated all of the strings in this app yet.

About the crash, the Wait For signature is incorrect. It should be:
B4X:
Wait For Msgbox_Result (result As Int)

Oh - right, of course. Dumb rookie mistake on my part. Weird though that it doesn't actually trigger an error unless there is another call in the same module that doesn't use a Wait For at all.

Anyway, I retested it with the correct signature on call 1 & no Wait For on call 2, as well as without it on both calls & it all seems to work ok - so thanks!

- Colin.
 
Upvote 0

ilan

Expert
Licensed User
Tip: use smart string literals for multiline text:
B4X:
sMsg = $"myPets v${Get_Version}

Copyright (c) 2015-2017 Computersmith
Icons created by Freepik (www.freepik.com)"$

About the crash, the Wait For signature is incorrect. It should be:
B4X:
Wait For Msgbox_Result (result As Int)

But if he only wants to use a simple msgbox without a result handler why does he needs a wait for call?

I dont see any reason for a crash in the code posted in post #1.

Its a simple msgbox and not msgbox2!!
 
Upvote 0
Top