0

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 extends 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>
1
  • Hey , i am having some problem with the java script call back. I tried your way but something is not working, can you please check once , Thank you in advance.Here is the link : stackoverflow.com/questions/20243686/… Commented Nov 27, 2013 at 13:52

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.