I’m using an AsyncTask to call some code in another class:
public class addNewLocation extends AsyncTask<HomerLocation, Integer, Void> {
@Override
protected Void doInBackground(HomerLocation... location) {
bridge.addNewLocation(location);
return null;
}
protected void onPostExecute(String result) {
System.out.println("Result after execution is : " + result);
}
}
I’m calling this via this command in a buttons code:
new addNewLocation().execute(newLocation,null,null);
newLocation being an Object with a few attributes which I set via user’s input in a dialog.
The line “bridge.addNewLocation(location); is trying to call the addNewLocation method in my bridge class passing a HomerLocation object called location as a parameter. However, I’m getting an error:
The method addNewLocation(HomerLocation) in the type HomerJSONBridge is not
applicable for the arguments (HomerLocation[])
The bridge class’s method is as follows:
public void addNewLocation(HomerLocation location) {
// code to add new location to homer
//HomerLocation location = locationArray;
client = new DefaultHttpClient();
StringBuilder url = new StringBuilder(HomerURL);
url.append("%7B%22operation%22%3A%22setLocation%22%2C%20%22name%22%3A%22"+location.getName()+
"%22%2C%20%22locationContextID%22%3A%22"+location.getContextID()+"%22%2C%20%22image%22%3A%22"+location.getIcon()+
"%22%2C%22design%22%3A%22"+location.getDesign()+"%22%7D");
}
This passes a JSON command to the servlet I’m accessing. If I rewrite the code in all places as follows:
protected Void doInBackground(HomerLocation... location) {
HomerLocation location1 = location[0];
bridge.addNewLocation(location1);
return null;
}
and
public void addNewLocation(HomerLocation[] locationArray) {
// code to add new location to homer
HomerLocation location = locationArray[0];
client = new DefaultHttpClient();
StringBuilder url = new StringBuilder(HomerURL);
url.append("%7B%22operation%22%3A%22setLocation%22%2C%20%22name%22%3A%22"+location.getName()+
"%22%2C%20%22locationContextID%22%3A%22"+location.getContextID()+"%22%2C%20%22image%22%3A%22"+location.getIcon()+
"%22%2C%22design%22%3A%22"+location.getDesign()+"%22%7D");
}
I still get a similar error (!) :
The method addNewLocation(HomerLocation[]) in the type HomerJSONBridge is
not applicable for the arguments (HomerLocation)
I’m really confused as it seems to be both a HomerLocation object and also a HomerLocation[]! Any help would be much appreciated.
The
...syntax in method parameters meanslocationis an array ofHomerLocation(HomerLocation[]), not a singleHomerLocationobject.If you’re sure it’s always gonna be one element wide, then quickest fix is: