1
votes

I'm developing an app that loads a HTML string into a WebBrowser, but when I call the LoadFromString methods from WebBrowser, it throws a RuntimeException with the message:

java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Thread-2'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 2) {c7ba400} called on null, FYI main Looper is Looper (main, tid 2) {c7ba400})

The HTML is stored in a file and loaded into a string just for test reasons, the final app will get the string from a DataSnap and show it using WebBrowser.

This is the code:

procedure LoadString;
var
  htmlContent: String;
  filePath: String;
  dbpath: String;
begin
  filePath := TPath.Combine(TPath.GetDocumentsPath, 'index.html');
  htmlContent := TFile.ReadAllText(filePath);
  WebBrowser1.LoadFromStrings(htmlContent, 'about:blank');
  btnSearch.Visible := False;
  TabControl1.GotoVisibleTab(tbResult.Index);
end;

I'm not using thread in this app.

If relevant, I'm using Delphi 10.1 Berlin and testing in a Moto G5 with Android 9.

1
In Berlin Delphi main thread is different from Android UI thread you may need to synchronize your WebView access with CallInUIThread call. Another possibility is that your LoadString is called from different thread than the one that created WebView. I don't have Berlin so I cannot verify and since Tokyo Delphi main thread is same as Android UI thread.Dalija Prasnikar
Where do I find the CallInUIThread? Is it within the System.Threading unit?MrLhama
I don't know. I don't have Berlin anymore. It is Android specific, so it can be somewhere in Android specific units.Dalija Prasnikar
Found it, its in FMX.Helpers.Android. Thank you so much, it workedMrLhama

1 Answers

1
votes

The WebBroser method needs to run in the UI Thread, so like Dalija Prasnikar comment said, I moved the call to CallInUiThread and everything is working now.