2011-02-26 40 views
3

我有以下基於asynctask的代碼塊。 我試圖通過返回LoadFeed() 返回一個List變量,doInBackground的返回類型是String。 如果我改變從字符串doInBackground的返回類型列出 然後我得到一個錯誤Android:無法指定列表<String> AsyncTask的返回類型:doInBackground

"The return type is incompatible with AsyncTask<String,Void,String>.doInBackground(String[])" 

如何解決這個問題? 請幫助

感謝,

private class DispData extends AsyncTask<String, Void, String> { 
    private final ProgressDialog dialog = new ProgressDialog(MessageList.this); 
    // can use UI thread here 
    protected void onPreExecute() { 
     dialog.setMessage("Fetching scores..."); 
     dialog.show(); 
    } 


    // automatically done on worker thread (separate from UI thread) 
    protected String doInBackground(final String... args) { 
     return loadFeed(); 

    } 


    // can use UI thread here 
    protected void onPostExecute(final List<String> result) { 
     if (dialog.isShowing()) { 
     dialog.dismiss(); 
     } 
    adapter = 
      new ArrayAdapter<String>(MessageList.this,R.layout.row,result); 
    MessageList.this.setListAdapter(adapter); 

    } 
} 

回答

16

類定義更改爲:

class DispData extends AsyncTask<String, Object, List<String>> 

這將迫使doInBackground聲明成爲:

protected List<String> doInBackground(String... arg); 
+0

感謝您的提示! – sammydude

1

首先,你真的應該添加@Override註釋您onPreExecute()doInBackground()onPostExecute()方法。在AsyncTask上,這對於幫助您跟蹤所有數據類型非常重要。

然後,如果你想doInBackground()返回List<String>,你需要改變它無論是在doInBackground()和你AsyncTask聲明(第三個數據類型將需要從String改變List<String>),用你的變化一起去,你已經提出到onPostExecute()

+0

新增的@overrides。在兩個地方都改變了retun數據類型。仍然收到「強制關閉」錯誤。 AsyncTask聲明中3個參數的意義是什麼?猜猜我需要知道這一點之前,我可以調試furthur。 TIA。 – sammydude

+0

對不起。有用!我的LoadFeed過程存在一些問題。將調試。 :-) – sammydude

+0

謝謝!完美工作! :-) – sammydude

相關問題