【问题标题】:I can't pass a String array to my AsyncTask我无法将字符串数组传递给我的 AsyncTask
【发布时间】:2012-06-13 07:08:06
【问题描述】:

编译器错误是“The method execute(ArrayList<String>...) in the type AsyncTask<ArrayList<String>,Void,ArrayList<String>> is not applicable for the arguments (String)

为什么它不接受新参数?谁能看到我做错了什么?

   ArrayList<String> passing = new ArrayList<String>();
            passing.add(logicalUrl);
            passing.add("filename.pdf");
            new myTask().execute(logicalUrl);
            return true;
        }

    public class myTask extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(ModuleContents.this);
            dialog.setTitle("Downloading...");
            dialog.setMessage("Please wait...");
            dialog.setIndeterminate(true);
            dialog.show();
        }

        protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
            ArrayList<String> passed = passing[0];
            String physicalUrl = parsePhysicalUrl(passed.get(0));
            String filename = passed.get(1);
            try {
                globals.saveFile(physicalUrl, filename);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return passed;

        }

【问题讨论】:

    标签: android


    【解决方案1】:

    你有new myTask().execute(logicalUrl)logicalUrl 是String,但你在泛型中指定应该是ArrayList&lt;String&gt;

    所以改成

     public class myTask extends AsyncTask<String, Void, ArrayList<String>> {}
    

    或添加作为您创建的类ArrayList 的参数。

    new myTask().execute(passing);
    

    现在它应该可以工作了。看来你只是忽略了它:]

    【讨论】:

      【解决方案2】:

      new myTask().execute(passing); 而不是new myTask().execute(logicalUrl);

      【讨论】:

        【解决方案3】:

        变化:

        new myTask().execute(logicalUrl);
        

        收件人:

        new myTask().execute(passing);
        

        【讨论】:

          【解决方案4】:

          你的方法应该是这样的

           ArrayList<String> passing = new ArrayList<String>();
                  passing.add(logicalUrl);
                  passing.add("filename.pdf");
          
                  **new myTask().execute(passing);**
          
                  return true;
          

          并检查此链接。,。它类似于你的问题

          Passing arguments to AsyncTask, and returning results

          【讨论】:

            猜你喜欢
            • 2013-02-15
            • 1970-01-01
            • 2012-03-07
            • 1970-01-01
            • 2018-10-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多