简体   繁体   English

我不确定为什么我的班级没有返回正确的类型。 数组列表 <HashMap<String,String> &gt;

[英]I am not sure why my class isn't returning the correct type. ArrayList<HashMap<String,String>>

I am not sure why I am getting the following error in my application: 我不知道为什么我的应用程序中出现以下错误:

Multiple markers at this line
    - implements 
     android.os.AsyncTask<java.lang.String,java.lang.Void,java.util.ArrayList<java.util.HashMap<java.lang.String,java.lang.String>>>.doInBackground
    - This method must return a result of type ArrayList<HashMap<String,String>>

When I hover over the return statement on line 195 I am getting the right type in the pop up window in Aptana. 当我将鼠标悬停在第195行的return语句上时,我在Aptana的弹出窗口中获得了正确的类型。

    public class getResults extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... params) {
        // TODO Auto-generated method stub

        //get names from each text box

        ArrayList<HashMap<String, String>> commonFilms = new ArrayList<HashMap<String, String>>();
        ArrayList<HashMap<String, String>> firstFilms = new ArrayList<HashMap<String, String>>();
        ArrayList<HashMap<String, String>> secondFilms = new ArrayList<HashMap<String, String>>();
        String nameOne = searchOne.getText().toString();
        String nameTwo = searchTwo.getText().toString();

        nameOne = nameOne.replace(" ", "_");
        nameTwo = nameTwo.replace(" ", "_");

        String searchOneURL = personURL + nameOne;
        String searchTwoURL = personURL + nameTwo;

        //Hashmap for ListView


        //Create JSON Parser Instanece
        JSONParser jParser = new JSONParser();

        //getting JSON string from url
        JSONObject jsonOne = jParser.getJSONFromUrl(searchOneURL);
        JSONObject jsonTwo = jParser.getJSONFromUrl(searchTwoURL);


        try {
            //Get ID of each person
            idOne = jsonOne.getJSONArray(TAG_ID);
            idTwo = jsonTwo.getJSONArray(TAG_ID);


            String firstID = null;
            String secondID = null;
            for(int i = 0; i < idOne.length(); i++){
                JSONObject iDeeOne = idOne.getJSONObject(i);

                //store each json item in variable
                firstID = iDeeOne.getString(TAG_ID);

            }

            for(int i = 0; i < idTwo.length(); i++){
                JSONObject iDeeTwo = idTwo.getJSONObject(i);

                //store each json item in variable
                secondID = iDeeTwo.getString(TAG_ID);
            }
            String creditURlBase = "http://api.themoviedb.org/3/person";

            String firstCreditURL = creditURlBase + firstID;
            String secondCreditURL = creditURlBase + secondID;

            JSONObject jSon = jParser.getJSONFromUrl(firstCreditURL);


            firstCast = jSon.getJSONArray(TAG_CAST);
            for(int i = 0; i < firstCast.length(); i++){
                JSONObject c = firstCast.getJSONObject(i);
                title = c.getString(TAG_TITLE);

                //ctreate new hashmap
                HashMap<String, String> map = new HashMap<String, String>();

                //and node to map
                map.put(TAG_TITLE, title);
                firstFilms.add(map);

            }

            secondCast = jSon.getJSONArray(TAG_CAST);
            for(int i = 0; i < secondCast.length(); i++){
                JSONObject c = firstCast.getJSONObject(i);
                title = c.getString(TAG_TITLE);

                //create hashmap
                HashMap<String, String> mapTwo = new HashMap<String, String>();

                mapTwo.put(TAG_TITLE, title);
                secondFilms.add(mapTwo);


            }

            if(firstFilms.size() > secondFilms.size()){
                for(int i = 0; i < firstFilms.size(); i++){
                    if(firstFilms.contains(secondFilms.get(i))){

                        HashMap<String, String> mapThree = new HashMap<String, String>();

                        mapThree.put(TAG_TITLE, secondFilms.get(i).toString());
                        commonFilms.add(mapThree);
                    }

                }

            }else{
                for(int i = 0; i < secondFilms.size(); i++){
                    if(secondFilms.contains(firstFilms.get(i))){
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_TITLE, firstFilms.get(i).toString());
                        commonFilms.add(map);
                    }
                }
            }
            return commonFilms;



            }
        catch(JSONException e){
            Log.e("Error", e.toString());
        }

        }


    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.totlayout, menu);
    return true;
}
}

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

That's because not all possible exit points of your method return an ArrayList . 那是因为并非所有可能的方法出口点都返回一个ArrayList In particular, if you ever get a JSONException : 特别是,如果您遇到JSONException

    catch(JSONException e) {
        Log.e("Error", e.toString());
    }

You need to either return null or an empty ArrayList . 您需要return null或空ArrayList Another alternative, as suggested by @BalusC, is to declare your method to throw a JSONException and either remove the try/catch block, or rethrow it: 另一种选择,如@BalusC所建议的,是声明你的方法抛出一个JSONException并删除try/catch块,或者重新抛出它:

    catch(JSONException e) {
        Log.e("Error", e.toString());
        throw e; // Possibly wrap it with a *domain* Exception
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:不确定为什么我的方法不返回ArrayList <HashMap<String, String> &gt; - Android:Not sure why my method isnt returning ArrayList<HashMap<String, String>> 为什么我收到警告:Class是原始类型。对泛型类型<T>的引用应该参数化“? - Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"? 不知道为什么带字符串的if语句不起作用 - Not sure why this if statement with string isn't working 我正在为我的独立班级研究二叉搜索树,但我不明白为什么这没有返回正确的高度 - I'm working on a binary search tree for my independent class and I can't figure out why this isn't returning the correct height HashMap vs ArrayList性能我是正确的 - HashMap vs ArrayList performance am I correct xcopy在我的批处理文件中不起作用,我不确定为什么吗? - xcopy isn't working in my batch file, and I am not sure why? 为什么我收到“列表的删除类型参数”错误 <String> list1 =新的ArrayList <String> ();” - Why i am getting “Remove type arguments” error for “List<String> list1 = new ArrayList<String>();” 为什么我的程序未返回正确的字符串值 - Why my program is not returning the correct string value 如果我使用HashMap <String, ArrayList<String> &gt;在Java中 - If I use HashMap<String, ArrayList<String>> in Java 为什么我的startActivityForResult()在转换ArrayList时返回null <String> 到字符串[]? - Why is my startActivityForResult() returning null when converting ArrayList<String> to String[]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM