0

I have a problem with my source code, I have write a javascript function that I active with a simple button into an HTML page.

The code look like this:

<html>
    <head>
        <base href="http://localhost:8080/testAndroidReturn/" target="_blank">
    </head>
    <body>
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="panel-title">
                        <h4>teste android</h4>
                    </div>
                </div>
                <div class="panel-body">
                    <input type="text" id="text" value="" />
                    <button type="submit" onclick="saveText()"> submit </button>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            function saveText(){
                alert("coucou");
                AndroidFunction.showToast("COUCOU");
            }
        </script>
    </body>
</html>

I have write a project in Android and in the main activity I have write this :

package com.bepark.myapplication.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        final MyJavaScriptInterface myJavaScriptInterface
                = new MyJavaScriptInterface(this);
        mWebView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://10.10.202.184:8080/testAndroidReturn/");
    }

    public class MyJavaScriptInterface {
        Context mContext;

        MyJavaScriptInterface(Context c) {
            mContext = c;
        }

        public void showToast(String toast){
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }

        public void openAndroidDialog(){
            AlertDialog.Builder myDialog
                    = new AlertDialog.Builder(MainActivity.this);
            myDialog.setTitle("DANGER!");
            myDialog.setMessage("You can do what you want!");
            myDialog.setPositiveButton("ON", null);
            myDialog.show();
        }

    }

}

But when I launch my android application and i click into my WebView in the button it do nothing and I have this error message to the log :

04-16 04:24:14.458    1212-1212/com.bepark.myapplication.app I/Choreographer﹕ Skipped 148 frames!  The application may be doing too much work on its main thread.
04-16 04:24:14.578    1212-1212/com.bepark.myapplication.app I/chromium﹕ [INFO:CONSOLE(22)] "Uncaught TypeError: Object [object Object] has no method 'showToast'", source: http://10.10.202.184:8080/testAndroidReturn/ (22)

and I don't understand why?

1 Answer 1

4

Try adding the annotation @JavascriptInterface to your public method if you're targetting Jelly Bean OSes.

Hope that helps.

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.