2013-02-20 76 views
0

我使用的AsyncTask調用在另一個類中的一些代碼:的AsyncTask doInBackground方法 - 參數誤差

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(位置);試圖調用addNewLocation方法在我的橋類路過稱爲位置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 []真糊塗!任何幫助將非常感激。

回答

1
protected Void doInBackground(HomerLocation... location) 

...語法方法參數意味着locationHomerLocationHomerLocation[])的陣列,而不是一個單一的HomerLocation對象。

如果你確定它總是會是一個元素寬,那麼最快的解決方法是:

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

啊太好了 - 以爲我試過這個。謝謝。 – 2013-02-20 15:02:22

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

我認爲這會幫助你。

相關問題