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?
B4X:
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
B4X:
java.lang.RuntimeException: Object should first be initialized (WebView).
 

drgottjr

Expert
Licensed User
Longtime User
this way:
B4X:
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: 146
Upvote 0

Sandman

Expert
Licensed User
Longtime User
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

drgottjr

Expert
Licensed User
Longtime User
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

Sandman

Expert
Licensed User
Longtime User
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.
B4X:
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
Top