开发者

Android App with PhoneGap: InvocationTarget Exception by calling addJavascriptInterface

开发者 https://www.devze.com 2023-02-24 05:52 出处:网络
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.

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!


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


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

精彩评论

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