I get the following error in Android Studio logcat: I / chromium: [INFO: CONSOLE (1)] "Uncaught ReferenceError: callJS not defined"
I researched stackoverflow and tried the suggested answers to a similar post without any luck. here
I am trying to execute javascript from a local html file loaded into WebChromeClient by clicking on the android button. The code is taken from the example of chapter 10 in the book "Pragmatic Programmers" "The fourth version of the book" Hello, Android, offering Google Mobile Development Platform ".
index.html
<html>
<head>
<script language="JavaScript">
function callJS(arg){
document.getElementById('replaceMe').innerHTML = arg;
{
</script>
</head>
<body>
<h2>WebView</h2>
<p>
<a href="#" onClick="window.alert('Alert from Javascript')">Display JavaScript alert</a>
</p>
<p>
<a href="#" onClick="window.android.callAndroid('Hello from Browser')">Call Android from JavaScript</a>
</p>
<p id="replaceMe"></p>
</body>
</html>
Mainactivity
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "LocalBrowser";
private final Handler handler = new Handler();
private WebView webView;
private TextView textView;
private Button button;
private class AndroidBridge {
@JavascriptInterface
public void callAndroid(final String arg) {
handler.post(new Runnable() {
public void run() {
Log.d(TAG, "callAndroid(" + arg + ")");
textView.setText(arg);
}
});
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_view);
textView = (TextView) findViewById(R.id.text_view);
button = (Button) findViewById(R.id.button);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AndroidBridge(),
"android");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(final WebView view,
final String url, final String message,
JsResult result) {
Log.d(TAG, "onJsAlert(" + view + ", " + url + ", "
+ message + ", " + result + ")");
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
result.confirm();
return true;
}
});
webView.loadUrl("file:///android_asset/index.html");
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "onClick(" + view + ")");
if (android.os.Build.VERSION.SDK_INT < 19) {
webView.loadUrl("javascript:callJS('Hello from Android')");
}else{
webView.evaluateJavascript("javascript:callJS('Hello from Android')", null);
}
}
});
}
}
activity_main
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1.0"
android:padding="5sp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="@string/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/call_javascript_from_android"
android:textSize="18sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
Any help is much appreciated! Thanks in advance!
source
share