我无法将String数组传递给我的AsyncTask

时间:2012-06-09 15:15:59

标签: android

编译器错误为“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;

        }

4 个答案:

答案 0 :(得分:2)

new myTask().execute(logicalUrl) logicalUrl为String,但您在通用中指定应为ArrayList<String>

所以将其改为

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

或添加您创建的班级ArrayList的参数。

new myTask().execute(passing);

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

答案 1 :(得分:2)

变化:

new myTask().execute(logicalUrl);

要:

new myTask().execute(passing);

答案 2 :(得分:1)

new myTask().execute(passing);代替new myTask().execute(logicalUrl);

答案 3 :(得分:1)

你的方法应该是这样的

 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