hi thank uYour 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.
#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
CallSubDelayed(Me,"after")
but can u please explain a bit for me about this story?
Does the Sub called by your javascript modify your activity UI?
If the answer is yes then you need to pass boolean true as callUIThread otherwise you can pass false.
If you pass false and then the Sub tries to modify your activity UI you will get an exception.
Sub after
Dim i As Int
i=123
End Sub
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
Thank u martin now undestoodThe 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.