Android Question error when using web view and webviewextra

ArminKH

Well-Known Member
hi what is this error?
 

ArminKH

Well-Known Member
This error just happen on android +5
my test device is galaxy s5
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Your javascript is using the CallSub command to call a b4a sub from javascript?
CallSub has a boolean parameter that must be set to true is the b4a sub (callled from javascript) modifies your activity's UI.
I suspect you are passing False instead of True to CallSub in your javascript.
 
Upvote 0

ArminKH

Well-Known Member
hi thank u
here is my code
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Dim JavaScript As WebViewExtras
    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1,0,0,100%x,100%y)
    JavaScript.addJavascriptInterface(WebView1,"B4A")
    WebView1.LoadUrl("http://gozine2.ir/Specialforms/viewapproximation.aspx")
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub WebView1_PageFinished (Url As String)
    Log("FINISHED")
    JavaScript.executeJavascript(WebView1,"B4A.CallSub('Process_HTML', false, document.documentElement.outerHTML)")
End Sub

Sub Process_HTML(Html As String)
    Log("Extracted")
    after
End Sub

Sub after
    JavaScript.executeJavascript(WebView1,"document.getElementById('MainContent_cmbField').value='"&"3"&"';")
    ToastMessageShow("set",False)
End Sub
when i using this code above error accured but when i call the "after" sub by using
B4X:
CallSubDelayed(Me,"after")
then my problem solved
i found this solution 1 hour ago after 25 compile
but i think as you said thats better if i set the FALSE TO TRUE in my code
but can u please explain a bit for me about this story?
thank u
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
but can u please explain a bit for me about this story?


The android user interface must only be modified by code running in the main application thread - it must not be modified by code running in a background thread.
The JavascriptInterface might or might not be executed in the main application thread - there is no guarantee that it will always be executed in any particular thread.

If your javascript calls a b4a sub that does not modify the UI then the value of the JavascriptInterface CallSub 'callUIThread' parameter is unimportant - a value of True or False can be used:

B4X:
Sub after
  Dim i As Int
  i=123
End Sub

The 'after' sub here does not modify the UI.
BUT your 'after' sub executes javascript that changes the content of a form field being displayed in the WebView - the UI is being modified.
So you must tell the JavascriptInterface that the UI is going to be modified in the sub called by CallSub.
The JavascriptInterface then ensures that the b4a sub is called in the main application thread.

So modify the javascript CallSub statement so it passes true as the callUIThread parameter and you'll have no need to use CallSubDelayed:

B4X:
Sub Process_Globals
End Sub

Sub Globals
    Dim JavaScript As WebViewExtras
    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1,0,0,100%x,100%y)
    JavaScript.addJavascriptInterface(WebView1,"B4A")
    WebView1.LoadUrl("http://gozine2.ir/Specialforms/viewapproximation.aspx")
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub WebView1_PageFinished (Url As String)
    Log("FINISHED")
    JavaScript.executeJavascript(WebView1,"B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)")
End Sub

Sub Process_HTML(Html As String)
    Log("Extracted")
    JavaScript.executeJavascript(WebView1,"document.getElementById('MainContent_cmbField').value='"&"3"&"';")
    ToastMessageShow("set",False)
End Sub

Martin.
 
Upvote 0

ArminKH

Well-Known Member
Thank u martin now undestood
Thanx for complete explain
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I'd guess that you're loading a webpage that tries to get the user's location but have not implemented the required sub in b4a.
Guessing that WebViewExtras tries to call a callback sub named ????_GeolocationPermissionsRequest, fails to find the sub and raises the null pointer exception.

Take a look at this post: https://www.b4x.com/android/forum/threads/webviewextras.12453/#post-102448
There's a few steps required in order to support webpages that request geolocation.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…