Android App with PhoneGap: InvocationTarget Exception by calling addJavascriptInterface
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");
}
}
精彩评论