I / chromium: [INFO: CONSOLE (1)] "Uncaught ReferenceError: callJS not defined"

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;

   /** Object exposed to JavaScript */
   private class AndroidBridge {
      @JavascriptInterface // Required in Android 4.2+
      public void callAndroid(final String arg) { // must be final
         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);

      // Find the Android controls on the screen
      webView = (WebView) findViewById(R.id.web_view);
      textView = (TextView) findViewById(R.id.text_view);
      button = (Button) findViewById(R.id.button);
      // Rest of onCreate follows...

      // Turn on JavaScript in the embedded browser
      webView.getSettings().setJavaScriptEnabled(true);

      // Expose a Java object to JavaScript in the browser
      webView.addJavascriptInterface(new AndroidBridge(),
            "android");

      // Set up a function to be called when JavaScript tries
      // to open an alert window
      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; // I handled it
         }
      });

      // Load the web page from a local asset
      webView.loadUrl("file:///android_asset/index.html");

      // This function will be called when the user presses the
      // button on the Android side
      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!

+4
source share

All Articles