Android Question CalledFromWrongThreadException Error

hanyelmehy

Active Member
Licensed User
Longtime User
i use this code
B4X:
Private Wve As WebViewExtras
Private MyWeb As WebView
Wve.addJavascriptInterface(MyWeb, "B4A")
Wve.addWebChromeClient(MyWeb,"")
Sub MyWeb_PageFinished (Url As String)
    Dim Javascript As String
    Javascript="B4A.CallSub('Process_HTML', false, document.documentElement.outerHTML)"
    Wve.executeJavascript(MyWeb, Javascript)
End Sub
every thing work fine in debug mode ,in release mode i get this error
B4X:
(CalledFromWrongThreadException) android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
texttrnaslate_process_html (java line: 841)
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6823)
    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:975)
    at android.view.ViewGroup.invalidateChild(ViewGroup.java:5125)
    at android.view.View.invalidateInternal(View.java:12774)
    at android.view.View.invalidate(View.java:12710)
    at android.widget.TextView.invalidateRegion(TextView.java:5405)
    at android.widget.TextView.invalidateCursor(TextView.java:5348)
    at android.widget.TextView.spanChange(TextView.java:8351)
    at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:10557)
    at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1053)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:759)
    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:658)
    at android.text.Selection.setSelection(Selection.java:76)
    at android.text.Selection.setSelection(Selection.java:87)
    at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:336)
    at android.widget.TextView.setText(TextView.java:4555)
    at android.widget.TextView.setText(TextView.java:4424)
    at android.widget.EditText.setText(EditText.java:84)
    at android.widget.TextView.setText(TextView.java:4379)
    at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:43)
    at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:214)
    at dic.test.texttrnaslate._process_html(texttrnaslate.java:841)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at uk.co.martinpearman.b4a.webviewextras.WebViewExtras$1B4AJavascriptInterface.CallSub(WebViewExtras.java:73)
    at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.os.HandlerThread.run(HandlerThread.java:61)
any idea how to solve this error ,Thank you
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
Not sure since I am not used to webviews, but the cause could be that the event (_PageFinished) is called from another thread (the one that was in charge of the task) and what you execute directly from it is not allowed to touch the UI thread (in debug mode the behavior will be different)

If this is the case, you could execute what is inside the 'MyWeb_PageFinished' in another Sub and invoke it with CallSubDelayed
B4X:
Private Wve AsWebViewExtrasPrivate MyWeb AsWebView
Wve.addJavascriptInterface(MyWeb, "B4A")
Wve.addWebChromeClient(MyWeb,"")

Sub WhatWillBeExecuted(Url as String)
   Dim Javascript AsString
   Javascript="B4A.CallSub('Process_HTML', false, document.documentElement.outerHTML)"
   Wve.executeJavascript(MyWeb, Javascript)
End Sub

Sub MyWeb_PageFinished (Url AsString)
   CallSubDelayed2(Me,"WhatWillBeExecuted",Url)
End Sub
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Thank you for your help ,but still have same error
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…