【问题标题】:Android: Unable to specify List<String> return type for AsyncTask:doInBackgroundAndroid:无法为 AsyncTask:doInBackground 指定 List<String> 返回类型
【发布时间】:2011-02-26 20:07:59
【问题描述】:

我有以下基于异步任务的代码块。 我正在尝试通过 return LoadFeed() 返回一个 List 变量 doInBackground 的返回类型是 String。 如果我将 doInBackground 的返回类型从 String 更改为 List 然后我得到一个错误

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

   }
}

【问题讨论】:

    标签: android list android-asynctask


    【解决方案1】:

    将您的类定义更改为:

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

    这将强制 doInBackground 声明变为:

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

    【讨论】:

      【解决方案2】:

      首先,您确实应该在您的onPreExecute()doInBackground()onPostExecute() 方法中添加@Override 注释。在AsyncTask 上,这对于帮助您跟踪所有数据类型至关重要。

      然后,如果您希望doInBackground() 返回List&lt;String&gt;,则需要在doInBackground()AsyncTask 声明中更改它(第三种数据类型需要从String 更改为@987654331 @),以配合您对 onPostExecute() 所做的更改。

      【讨论】:

      • 添加了@overrides。在两个地方都更改了 retun 数据类型。仍然收到“强制关闭”错误。 AsyncTask声明中3个参数的意义是什么?我想我需要知道这一点,然后才能进一步调试。 TIA。
      • 对不起。有用!我的 LoadFeed 程序存在一些问题。将调试。 :-)
      • 谢谢!工作完美! :-)
      猜你喜欢
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2022-09-30
      • 1970-01-01
      相关资源
      最近更新 更多