Android Question error :android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat

hanyelmehy

Active Member
Licensed User
Longtime User
When i try to make this code
B4X:
Sub WebView1_PageFinished (Url As String)
    Dim Javascript As String
    Javascript="B4A.CallSub('ProcessHTML', false, document.documentElement.outerHTML)"
    WebViewExtras1.executeJavascript(MyWebV, Javascript)
End Sub
Sub ProcessHTML(Html As String)
    ProcessHtmlData(Html)
    Label1.Text="Any Thing"
End Sub
i get error when try to write any thing to label (i try to add label using code or using Activity.LoadLayout)
B4X:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
any help
Thank you
 

mangojack

Expert
Licensed User
Longtime User
from what I was able to find out after getting the same error .. B4A.CallSub runs on a non GUI thread , whereas all code involving GUI views etc .. must be run on the Main thread.

This was solved by downloading the threading library Here ...

then ...
B4X:
Sub Globals
   Private Thread1 As Thread   'maybe should be Process_Globals ?
End Sub

Sub ProcessHTML(Html AsString)
  ProcessHtmlData(Html)

  Thread1.Initialise("Thread1")
  Thread1.RunOnGuiThread("Process_Views" , Null)
End Sub

Sub Process_Views
  Label1.Text="Any Thing"
End Sub
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
As a follow up I found this ... I have not yet changed my code and tested.

In the webpage loaded in the WebView you must be using javascript such as:

B4X:
B4A.CallSub('ProcessHTML', false, <some-value-here>)
Change the boolean parameter in the javascript to true:
B4X:
B4A.CallSub('ProcessHTML', true, <some-value-here>);
The ProcessHTML Sub will now be executed on the main UI thread, not a background thread.
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Thank you
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…