WebViewCoreThread fatal exception
coder4567 opened this issue · comments
I would like to thank you for this solution, it is very helpful. I'm trying to integrate this solution in my project, but I'm still new to android, what I'm trying to do is to run the following function from the webview class I have, and its triggered by a button. The problem is when it reaches popoverView.showPopoverFromRectInViewGroup it gives WebViewCoreThread fatal exception. android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
NB: I have changed the textWebView in popover_showed_view into a webview cause I have html content to show.
public void openPopOverWithContent(String content, Rect displayR)
{
PopoverView popoverView = new PopoverView(context, R.layout.popover_showed_view);
popoverView.setContentSizeForViewInPopover(new Point(320, 340));
popoverView.setDelegate(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, "hello", "text/html", "utf-8", null);
Rect viewRect = new Rect(300, 300, 300, 320);
popoverView.showPopoverFromRectInViewGroup(this, viewRect, PopoverView.PopoverArrowDirectionAny, true);
}
Thanx in advance
Ok, let me go trough it, and see what it is.
thank u, I truly appreciate it, I've been debugging and noticed that the problem occurs when it reaches: group.addView(this, insertParams); in showPopoverFromRectInViewGroup function.
thought this would help to find the bug
By the way I'm calling this function from inside the class of the webview that I created. cause if I wanted to call it from the activity, I have to make the function static which would add so many restrictions to my code, and I can't use "this", nor "getContext()" ...
In popoverView.showPopoverFromRectInViewGroup(this, viewRect, PopoverView.PopoverArrowDirectionAny, true);
"this" might be referring to the webview class, could it be the problem cause? and if it is how can I handle it and what should I put instead of "this"
Ok, I made a test, and I can load a Webview with an HTML string inside the popover just fine, from the main Activity
From what I can read, you are calling the showPopoverFromRectInViewGroup in a different thread than the main thread (in this case in the WebViewCoreThread).
As far as I know, the main rule for Android development is to call all the view methods (adding views, removing views, showPopoverFromRectInViewGroup) in the main thread, while processing, data downloading, etc... is done in background threads (like user-created Threads, or WebViewCoreThread in this case).
I don't know what your WebView class does, or when does it call showPopoverFromRectInViewGroup, but you should call this method in the main thread, since it's updating the views and the interface.