javascript - WebView methods on same thread error -
i have android program (java + html in webview). can call javascript java code. other way around stopped working (after updating in eclipse).
so i'm trying do
- make webview (worked)
- calling in javascript androidfunction.test(); (worked)
- the java test() function call webview.loadurl("javascript:helloback()"); (! not working anymore)
i tried let work webview in mainactivity, didnt work.
mainactivity.java
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final webview webview = (webview)findviewbyid(r.id.webview); webview.getsettings().setjavascriptenabled(true); webview.setwebchromeclient(new webchromeclient()); websettings websettings = webview.getsettings(); websettings.setjavascriptenabled(true); javascr = new javascript(this, webview); webview.addjavascriptinterface(javascr, "androidfunction"); webview.loadurl("file:///android_asset/www/index.html"); .... }
javascript.java
public class javascript { context cont; webview webview; javascript(context c, webview w) { cont = c; webview = w; } // function called in javascript androidfunction.test(); public void test() { // breaking point!!! webview.loadurl("javascript:helloback()"); }
error:
03-24 11:47:50.103: w/webview(21026): @ com.android.org.chromium.base.systemmessagehandler.handlemessage(systemmessagehandler.java:27) 03-24 11:47:50.103: w/webview(21026): java.lang.throwable: webview method called on thread 'javabridge'. webview methods must called on same thread. (expected looper looper{41ab68f8} called on looper{41bb70a8}, fyi main looper looper{41ab68f8}) 03-24 11:47:50.103: w/webview(21026): @ android.webkit.webview.checkthread(webview.java:2063) 03-24 11:47:50.103: w/webview(21026): @ android.webkit.webview.loadurl(webview.java:794) 03-24 11:47:50.103: w/webview(21026): @ com.example.hellobt.javascript.test(javascript.java:24) 03-24 11:47:50.103: w/webview(21026): @ com.android.org.chromium.base.systemmessagehandler.nativedorunlooponce(native method) 03-24 11:47:50.103: w/webview(21026): @ com.android.org.chromium.base.systemmessagehandler.handlemessage(systemmessagehandler.java:27) 03-24 11:47:50.103: w/webview(21026): @ android.os.handler.dispatchmessage(handler.java:102) 03-24 11:47:50.103: w/webview(21026): @ android.os.looper.loop(looper.java:137) 03-24 11:47:50.103: w/webview(21026): @ android.os.handlerthread.run(handlerthread.java:61)
thanks answer. edited function in javascript file this:
private void test(final string s) { webview.post(new runnable() { public void run() { webview.loadurl("javascript:" + s + ";"); } }); system.out.println("javscript done.."); }
the javascript method executed on background (i.e. non-ui) thread. need call android view related methods on ui thread. can achieve need with:
mwebview.post(new runnable() { @override public void run() { mwebview.loadurl(...). } });
which post task run on ui thread.
Comments
Post a Comment