admin管理员组文章数量:1310092
I have a WebView
in an app that loads a specific web page.
On that web page is a button that uses JavaScript to call a method within the Android activity to reload the URL in the WebView (effectively resetting the web app to its default state).
I've got all of the JavaScript interface to Android bit working thanks to a few threads here and I can pop up a Toast
to show that the app is about to reload, but I'm getting an error with the WebView.loadUrl() call as follows.
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {1d015c84} called on Looper (JavaBridge, tid 122148) {33703881}, FYI main Looper is Looper (main, tid 1) {1d015c84})
I'm guessing that this is because the method that is doing the reload is not within the onCreate
method for this activity which does all of the other WebView
stuff but I'm not sure if this really is the problem and how to resolve it if it is - as the method that reloads the URL needs to be within the JavascripInterface class to be reachable from within the web-page.
I have a WebView
in an app that loads a specific web page.
On that web page is a button that uses JavaScript to call a method within the Android activity to reload the URL in the WebView (effectively resetting the web app to its default state).
I've got all of the JavaScript interface to Android bit working thanks to a few threads here and I can pop up a Toast
to show that the app is about to reload, but I'm getting an error with the WebView.loadUrl() call as follows.
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {1d015c84} called on Looper (JavaBridge, tid 122148) {33703881}, FYI main Looper is Looper (main, tid 1) {1d015c84})
I'm guessing that this is because the method that is doing the reload is not within the onCreate
method for this activity which does all of the other WebView
stuff but I'm not sure if this really is the problem and how to resolve it if it is - as the method that reloads the URL needs to be within the JavascripInterface class to be reachable from within the web-page.
1 Answer
Reset to default 11Loading a page in a WebView
ponenet (or reloading) needs to be done on the UI thread for some reason, so simply wrapping the reload call in runOnUiThread
resolves this issue.
@JavascriptInterface
public void reloadSite(){
Toast.makeText(mContext, getString(R.string.reloadingWebApp), Toast.LENGTH_LONG).show();
runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl(getString(R.string.web_app_url));
}
});
}
本文标签: javaReloading a WebView with a JavaScript call from a loaded web pageStack Overflow
版权声明:本文标题:java - Reloading a WebView with a JavaScript call from a loaded web page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741853336a2401196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论