开发者

I cannot call an android function from javascript

I just started make a kind of Android hybrid app, an application with WebView. As a first step, I tried to call a function of Android code from JavaScript. But, the app keeps crashing while I am at it.

All I did was setting up WebView, adding a class that is used from JavaScript code, then calling the function from JavasScript.

Thanks in advance.

The android codes are following:

public class Grand_ride_androidActivity e开发者_StackOverflow社区xtends Activity {
    WebView mWebView;
...
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);


        mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        mWebView.loadUrl(Config.url);
    }
}

public class JavaScriptInterface {
    Context mContext;

    JavaScriptInterface(Context c) {
        mContext = c;
    }

    public void ensureTracking(Integer rideId) {
        //do nothing
    }
}

And the HTML is following:

...
<body>

<script type="text/javascript">
//<![CDATA[
        Android.ensureTracking(97);
//]]>
</script><div>
        <p>You have checked in at 03:44PM</p>
        <a href="/rides/97/check_out">Check out</a>
</div>
</body>


I think you can only use primitive types and String, so ensureTracking(Integer rideId) should be ensureTracking(int rideId).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜