温馨提示:本文翻译自stackoverflow.com,查看原文请点击:android - TWebBrowser throwing thread exception when loading html from string
android delphi firemonkey delphi-10.1-berlin

android - 从字符串加载html时TWebBrowser抛出线程异常

发布于 2020-03-27 11:20:28

我正在开发一个将HTML字符串加载到WebBrowser中的应用程序,但是当我从WebBrowser调用LoadFromString方法时,它将引发RuntimeException和以下消息:

java.lang.RuntimeException:java.lang.Throwable:在线程“ Thread-2”上调用了WebView方法。所有WebView方法必须在同一线程上调用。(预期的Looper Looper(主要,tid 2){c7ba400}调用为null,FYI主要Looper是Looper(主要,tid 2){c7ba400})

HTML只是出于测试原因而存储在文件中并加载到字符串中,最终的应用程序将从DataSnap中获取字符串,并使用WebBrowser对其进行显示。

这是代码:

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;

我没有在此应用程序中使用线程。

如果相关,我正在使用Delphi 10.1 Berlin,并在具有Android 9的Moto G5中进行测试。

查看更多

查看更多

提问者
MrLhama
被浏览
171
MrLhama 2019-07-03 22:38

WebBroser方法需要在UI线程中运行,因此就像Dalija Prasnikar的评论所说,我将调用移至CallInUiThread,现在一切正常。