AsyncTask doInBackground方法 - 参数错误

时间:2013-02-20 13:24:11

标签: android android-asynctask

我正在使用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是一个具有一些属性的Object,我通过对话框中的用户输入设置了这些属性。

“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 []!任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

protected Void doInBackground(HomerLocation... location)

方法参数中的...语法意味着locationHomerLocationHomerLocation[])的数组,而不是单个HomerLocation对象。

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

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

答案 1 :(得分: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");
}

我认为这会对你有帮助。

相关问题