executing JS functions from a Phonegap Android Plugin
-
28-10-2019 - |
Question
I'm making a Phonegap Android app using Phonegap v1.3. I wrote a plugin for phonegap which gets a particular file from the system and then executes a js function.
As all plugins extend the Phonegap class Plugin, i can't execute a js function directly from there. so from the execute function of the Plugin, i execute -->
PluginResult result = null;
Home home = new Home();
result = new PluginResult(Status.OK);
home.loadPage("pic.jpg");
In this the Home is my main class which extends Droidgap.
In home i have the function loadPage
public void loadPage(String filePath){
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
super.loadUrl("javascript:navigator.somefunc(\""+filePath+"\");");
}
The problem is that the setIntegerProperty function of DroidGap is throwing an NPE. Here are the logs.
01-16 11:41:32.364: W/System.err(13162): java.lang.NullPointerException
01-16 11:41:32.364: W/System.err(13162): at com.phonegap.DroidGap.setIntegerProperty(DroidGap.java:738)
01-16 11:41:32.364: W/System.err(13162): at e2o.mobile.Home.loadSharePage(Home.java:120)
01-16 11:41:32.364: W/System.err(13162): at com.phonegap.plugins.checkImage.CheckUploadImage.execute(CheckUploadImage.java:158)
01-16 11:41:32.364: W/System.err(13162): at com.phonegap.api.PluginManager$1.run(PluginManager.java:150)
01-16 11:41:32.364: W/System.err(13162): at java.lang.Thread.run(Thread.java:1019)
Bascically in the setIntegerProperty function -
public void setIntegerProperty(String name, int value) {
this.getIntent().putExtra(name, value);
}
the this.getIntent() is returning null. can u pls tell me what am i missing. Or in my use case scenario, how do i execute that js function. If i dont use
super.setIntegerProperty("loadUrlTimeoutValue", 60000, intent);
then also the loadUrl does nothing. the JS does not get called, or it times out.
Solution
It is because you are instantiating a new Home object. The object is created using the constructor but the onCreate method is not called so a lot of the proper object setup doesn't occur. That is why you are getting the NullPointerException.
You are returning a new PluginResult with a status of OK so the success function of the can execute the navigator.somefunc(filePath) method for you. It'll be a lot easier that way.