B4J Question Sender not set in WebView_LocationChanged event

Sandman

Expert
Licensed User
Longtime User
I have a layout with several WebViews, each containing html with a link. Clicking a link triggers the LocationChanged event just fine. However, I can't figure out how to get which WebView the click happened in, as Sender is Null. Is this expected behaviour on B4J or have I misunderstood something?
Code: Copy
Public Sub webContent_LocationChanged (Location As String)
    Log("Location changed: " & Location)
    Sleep(0)
    Log(Sender)
    Sender.As(WebView).LoadHtml("<h1>Beep boop</h1>")
End Sub

Logging the Sender shows it's Null.

And casting as WebView obviously then gives error
Code: Copy
java.lang.RuntimeException: Object should first be initialized (WebView).
 
this way:
B4X: Copy
Sub wv_LocationChanged (Location As String)
    Dim webview As WebView = Sender
    Log("sender " & webview.Tag)
End Sub
 

Attachments

  • sandman.png
    sandman.png
    23 KB · Views: 451
Upvote 0
Doesn't work for me. It's not the method of casting that is the issue, it's that Sender is Null.

In your case it's obviously not Null, but in my case it is. I can't understand what would make an event not have a Sender, as it's the cause for the event even happening. Any ideas for that?
 
Upvote 0
sleep() kills the sender. if i add sleep() to my example, sender becomes null. sender is only valid within the sub (see erel's documentation). sleep() alters that situation.
 
Upvote 1
Tested, and verified - it worked fine!

For future forum members, I simply did this, which stores a local variable containing the Sender (as a WebView) before the Sleep.
Code: Copy
Public Sub webContent_LocationChanged (Location As String)
    Dim tmpWebView As WebView = Sender
    Log("Location changed: " & Location)
    Sleep(0)
    Log(tmpWebView)
    tmpWebView.LoadHtml("<h1>Hello</h1>")
End Sub

sleep() kills the sender. (...) sender is only valid within the sub (see erel's documentation).
Yes, I think it's fairly well known that Sender only is available in the sub, but I can't remember seeing anything about Sleep killing it. You mention Erel's documentation of Sender, but even though I've searched all over I can't seem to find anything that mentions the Sleep-killer. Can you please post a link?
 
Upvote 0
Back
Top