2

I am using the Android SDK and PhoneGap to create a native Android App. Now I want to use Java methods in the view, calling by JS methods.

In the Main Class I called the "addJavascriptInterface" method to bind a java class with the view.

public class App extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");        

        super.loadUrl("file:///android_asset/www/index.html");
    }
}

The problem is I get an InvocationTarget Exception when the programm executes the line "appView.addJavascript..." and the program crashes on the device.

Any solutions here?

Thanks!

2 Answers 2

1

Calling Java methods from JavaScript is exactly what PhoneGap plugins enable. Checkout several examples here

1

Using addJavascriptInterface before super.init(); can make the application crash.

You should try the following :

public class App extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.init();

        appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");        

        super.loadUrl("file:///android_asset/www/index.html");
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.