簡體   English   中英

我不確定為什么我的班級沒有返回正確的類型。 數組列表 <HashMap<String,String> &gt;

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

我不知道為什么我的應用程序中出現以下錯誤:

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

任何幫助,將不勝感激。 謝謝

那是因為並非所有可能的方法出口點都返回一個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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM