Loren
Loren

Reputation: 340

The method addJavascriptInterface(WebViewActivity.JavaScriptInterface, String) is undefined for the type CordovaWebView

I am not able to open link in Cordova webview on HTML button click. It shows "The method addJavascriptInterface(WebViewActivity.JavaScriptInterface, String) is undefined for the type CordovaWebView"

public class WebViewActivity extends CordovaActivity{


     JavaScriptInterface JSInterface;
     CordovaWebView mainView;

    @SuppressLint("SetJavaScriptEnabled") public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         super.init();
        setContentView(R.layout.webview);

        mainView  = (CordovaWebView) findViewById(R.id.webView1);

        JavaScriptInterface jsInterface = new JavaScriptInterface(this);
        mainView.getSettings().setJavaScriptEnabled(true);
        mainView.addJavascriptInterface(jsInterface, "JSInterface");
        System.out.println("Opening webview");
        mainView.loadUrl("file:///android_asset/index.html" );


    }
     public class JavaScriptInterface {
            Context mContext;

            /** Instantiate the interface and set the context */
            JavaScriptInterface(Context c) {
                mContext = c;
            }

            public void changeActivity()
            {
                Log.d("In webviewActivity", "Inside change activity");
                Intent i = new Intent(WebViewActivity.this, Google.class);
                startActivity(i);
                Log.d("After Intent", "Opening google class");
                finish();
            }
        }

     }

Upvotes: 2

Views: 4243

Answers (1)

Ángel B.
Ángel B.

Reputation: 353

what version of cordova are you using?

I had the same problem with cordova 5.1, I recommend you to see this answer:

Accessing appView from Cordova 5.0.0

There you can see the method to access to your appview object and add the javascriptinterface.

Hope It helps!

Upvotes: 1

Related Questions