Sandman Expert Licensed User Longtime User Jul 5, 2022 #1 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).
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).
drgottjr Expert Licensed User Longtime User Jul 5, 2022 #2 this way: B4X: Copy Sub wv_LocationChanged (Location As String) Dim webview As WebView = Sender Log("sender " & webview.Tag) End Sub Attachments sandman.png 23 KB · Views: 451 Upvote 0
this way: B4X: Copy Sub wv_LocationChanged (Location As String) Dim webview As WebView = Sender Log("sender " & webview.Tag) End Sub
Sandman Expert Licensed User Longtime User Jul 5, 2022 #3 drgottjr said: this way Click to expand... 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 said: this way Click to expand... 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?
drgottjr Expert Licensed User Longtime User Jul 5, 2022 #4 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
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.
Sandman Expert Licensed User Longtime User Jul 5, 2022 #5 Oh wow, I don't think I knew that! I'll report back when I've tested. ? Upvote 0
Sandman Expert Licensed User Longtime User Jul 6, 2022 #6 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 drgottjr said: sleep() kills the sender. (...) sender is only valid within the sub (see erel's documentation). Click to expand... 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
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 drgottjr said: sleep() kills the sender. (...) sender is only valid within the sub (see erel's documentation). Click to expand... 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?