So, I know that there are other messages dedicated to the same problem, and I think that I followed them in the letter, but, alas, to no avail. I am trying to learn how to get my application to interact with javascript. I just want to return the value from my javascript to my activity.
here is my activity:
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);
Here is the html file uploaded by my WebView that contains the javascript I want to run:
<!DOCTYPE html> <html> <head> <script language="javascript"> accessor.setValue('Hello!'); </script> </head> <body> <h1>Testing</h1> </body> </html>
This does not work. When I run the code using "Log.d" ("YO!", MString); uncommented I get a null pointer exception, which means that javascript never assigned the value mString. what am I doing wrong?
javascript android webview
aamiri
source share