我不确定为什么我的班级没有返回正确的类型。 ArrayList的<散列映射<字符串,字符串>&GT; </散列映射<字符串,字符串>

时间:2012-09-11 01:32:54

标签: java android

我不确定为什么我的申请中出现以下错误:

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>>

当我将鼠标悬停在第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;
}
}

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:4)

那是因为并非方法的所有可能退出点都返回ArrayList。特别是,如果你得到JSONException

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

您需要return null或空ArrayList。正如@BalusC所建议的另一个替代方法是声明你的方法抛出JSONException并删除try/catch块,或者重新抛出它:

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