I am trying to use a webview with an input box, take the string, and pass it to a second activity. For some reason, the button is not doing anything and I get the error:
Uncaught TypeError: Object [object Object] has no method 'changeActivity' at file:///android_asset/www/index.js:3
So my HTML says this:
<input id="name" value="" />
<button onclick="checkMovie()"> Check it! </button>
my JS says this:
function checkMovie() {
var movieName = document.getElementById('name').value;
webapi.changeActivity(movieName);}
and my Android code says this:
public class MainActivity extends Activity implements OnClickListener {
// Setting up known variables
JavaScriptInterface JSInterface;
Button find;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// changing main layout to show the splash first
setContentView(R.layout.activity_main);
// Tie my webviews and set up the webview to handle JS
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
// Expecting UI
webView.setWebChromeClient(new WebChromeClient());
// allowing to inject Java objects into a page's JavaScript
webView.addJavascriptInterface(new JavaScriptInterface(this), "webapi");
// Load the local file
webView.loadUrl("file:///android_asset/www/index.html");
find = new Button(MainActivity.this);
find.setOnClickListener(this);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
// Add the interface to record javascript events
webView.addJavascriptInterface(find, "find");
}
public class JavaScriptInterface {
Context mContext;
// Instantiate the interface and set the context
JavaScriptInterface(Context c) {
mContext = c;
}
// Call the function changeActivity defined in my JS of my HTML
public void changeActivity(String movieName) {
// Call the Movie App and store the intent to pass it onto the app.
Log.e("XXXXXXXXXXXXXXX", "X==========>" + movieName);
Intent i = new Intent(MainActivity.this, second.class);
i.putExtra("movie_name", movieName);
startActivity(i);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(find)) {
Log.e("XXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXX");
}
}
}