Android WebView - Javascript不会触发回调函数

时间:2012-04-11 10:34:01

标签: javascript android android-webview

我是Android新手并且正在尝试设置Android Webview以加载javascript函数并将结果返回给回调函数。

请参阅以下代码

public class AWebViewActivity extends Activity {

    /**
     * Defines an Interface to call Syntax Highlighting Javascript and return the result
     * to a string by calling the call back function.
     */
     public class JavaScriptInterface {
         Context mContext;
         JavaScriptInterface(Context c) {
             mContext = c;
         }

         //add other interface methods to be called from JavaScript
         // This annotation is required in Jelly Bean and later:
         @JavascriptInterface
         public void receiveValueFromJs(String str) {
              //do something useful with str
              Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_LONG).show();
         }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Try WebView
        WebView webView = (WebView) findViewById(R.id.editor_view);
        //webView.setWebChromeClient(new WebChromeClient());

        webView.getSettings().setJavaScriptEnabled(true);

        // RiNxX's suggestion
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url){
                view.loadUrl("javascript:getValue()");
            }
        });

        webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");
        webView.loadUrl("file:///mnt/sdcard/a.html");

        //SystemClock.sleep(1000);
        //webView.loadUrl("javascript:getValue()");
    }
}

HTML文件如下:

<html>
    <head>
    </head>
    <body>
        <script type="text/javascript">
            function getValue()
            {
                var val="Amith Raravi";
                MyAndroid.receiveValueFromJs(val);
            }
        </script>
    Hey ya
    </body>
</html>

我还添加了INTERNET权限作为Manifest元素的子项。我浏览了StackOverflow上发布的其他问题,并将脚本移动到了HTML的主体。

在我添加了RiNxX建议的更改后,应用程序关闭(它没有执行回调),我在LogCat中收到以下警告

JNI WARNING: jarray 0x405414e8 points to non-array object (Ljava/lang/String;)
I/dalvikvm(361): "WebViewCoreThread" prio=5 tid=9 NATIVE
I/dalvikvm(361):   | group="main" sCount=0 dsCount=0 obj=0x4051d580 self=0x29a1a8
I/dalvikvm(361):   | sysTid=370 nice=0 sched=0/0 cgrp=default handle=2728672
I/dalvikvm(361):   | schedstat=( 277421205 460097188 88 )
I/dalvikvm(361):   at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
I/dalvikvm(361):   at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
I/dalvikvm(361):   at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:246)
I/dalvikvm(361):   at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1570)
I/dalvikvm(361):   at android.webkit.WebViewCore.access$1400(WebViewCore.java:53)
I/dalvikvm(361):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:956)
I/dalvikvm(361):   at android.os.Handler.dispatchMessage(Handler.java:99)
I/dalvikvm(361):   at android.os.Looper.loop(Looper.java:123)
I/dalvikvm(361):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
I/dalvikvm(361):   at java.lang.Thread.run(Thread.java:1019)
E/dalvikvm(361): VM aborting

我已经待了一天多了。我们非常欢迎任何帮助:)

4 个答案:

答案 0 :(得分:5)

显然,这是2.3.3模拟器中的一个长期存在的错误。

请参阅以下链接。

http://groups.google.com/group/android-developers/browse_thread/thread/992ca132e15745ec/da8da866d107e8b3?pli=1

目前正在下载2.2和3.0模拟器。

感谢您提供所有帮助,以及精彩的论坛。

我会回来提出更多问题!

答案 1 :(得分:2)

我遇到了同样的问题,但是当我尝试从资产加载文件时工作正常:

webView.loadUrl("file:///android_asset/a.html");

答案 2 :(得分:1)

添加在加载页面时调用javascript界面​​的webview客户端

webview.setWebViewClient(new WebViewClient() {
@Override    
public void onPageFinished(WebView view, String url) {    
   view.loadUrl("javascript:getValue()");    
}   
webview.loadUrl(SOME_WEBPAGE);

答案 3 :(得分:0)

java脚本从android webview调用

webView.loadUrl("javascript:setDatePickervalue(\""
                        + pselSelectedDateProp.getFormatedDate() + "\")");


Java脚本函数

        <script type="text/javascript" language="JavaScript">
        function setDatePickervalue(selecteddate){          
                document.getElementById("text").innerHTML = selecteddate.toString();            
                //alert('hello' + selecteddate.toString());

            }
  </script>

.... 我试图像你一样实施同样的事情...... 它给了我vm在模拟器中中止异常...但它在设备上工作很精细..