I / chromium:[INFO:CONSOLE(1)]" Uncaught ReferenceError:callJS未定义"

时间:2015-07-02 20:28:59

标签: javascript android webchromeclient

我在Android Studio logcat中收到以下错误: I / chromium:[INFO:CONSOLE(1)]" Uncaught ReferenceError:callJS未定义"

我已经研究过stackoverflow并尝试了类似帖子的建议答案而没有运气。 here

我试图通过点击Android按钮从加载到WebChromeClient的本地html文件中执行javascript。该代码取自The Pragmatic Programmers" Hello Android Introducing Google&the 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>

非常感谢任何帮助!提前谢谢!

0 个答案:

没有答案