0

我正在使用 AsyncTask 调用另一个类中的一些代码:

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);
    }
}

我在按钮代码中通过此命令调用它:

new addNewLocation().execute(newLocation,null,null);

newLocation 是一个具有一些属性的对象,我通过用户在对话框中的输入来设置这些属性。

“bridge.addNewLocation(location); 行正在尝试调用我的网桥类中的 addNewLocation 方法,并传递一个名为 location 的 HomerLocation 对象作为参数。但是,我收到一个错误:

The method addNewLocation(HomerLocation) in the type HomerJSONBridge is not 
applicable for the arguments (HomerLocation[])

桥接类的方法如下:

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");
}

这会将 JSON 命令传递给我正在访问的 servlet。如果我在所有地方重写代码如下:

protected Void doInBackground(HomerLocation... location) {
        HomerLocation location1 = location[0];
        bridge.addNewLocation(location1);
        return null;
    }

 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");
}

我仍然收到类似的错误(!):

The method addNewLocation(HomerLocation[]) in the type HomerJSONBridge is 
not applicable for the arguments (HomerLocation)

我真的很困惑,因为它似乎既是 HomerLocation 对象又是 HomerLocation[]!任何帮助将非常感激。

4

2 回答 2

1
protected Void doInBackground(HomerLocation... location)

...方法参数中的语法表示location是一个HomerLocation( HomerLocation[]) 数组,而不是单个HomerLocation对象。

如果您确定它总是一个元素宽,那么最快的解决方法是:

@Override
protected Void doInBackground(HomerLocation... location) {
    bridge.addNewLocation(location[0]);
    return null;
}
于 2013-02-20T13:27:07.887 回答
1

像这样尝试因为您的 HomerLocation... 位置是 HomerLocation 对象的数组。

protected Void doInBackground(HomerLocation... location) {

    bridge.addNewLocation(location[0]);
    return null;
}

public void addNewLocation(HomerLocation locationArray) {

   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");
}

我想这会对你有所帮助。

于 2013-02-20T13:33:58.500 回答