Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid- using addJavaScriptInterface to return a value from Javascript
    primarykey
    data
    text
    <p>So i know there are other posts addressing the same issue, and I think I've followed them to the letter but alas to no avail. I'm trying to learn how to get my app to interact with javascript. I just want to be able to return a value from my javascript to my activity.<br> here is my activity:</p> <pre><code>public class JSExample extends Activity { WebView mWebView; String mString; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.mWebView); mWebView.addJavascriptInterface(new ClsAccessor(), "accessor"); String html = getAssetsContent("jsinterface.html"); mWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null); // Log.d("YO!", mString); } private String getAssetsContent(String filename){ ..... } private void closeStream(BufferedReader stream) { ..... } class ClsAccessor{ public void setValue(String value){ JSExample.this.mString = value; } } </code></pre> <p>Here is the html file loaded by my WebView that contains the javascript i want to run:</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script language="javascript"&gt; accessor.setValue('Hello!'); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Testing&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This does not work. When i run the code with "Log.d("YO!", mString);" uncommented I get a null pointer exception, which means that the javascript never assigned a value to mString. what am i doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload