arraylist似乎是空出来的功能

时间:2017-09-28 17:14:39

标签: java android listview layout

嗨我有一个数组列表,它在类的开头定义,我也尝试将对象放在其中,以便我可以将它用于自定义arrayAdapter的列表视图但是当我想使用它时似乎是空的在响应之外侦听器我用两个Toast测试它forst一个运行没有问题第二个抛出IndexOutOfBoundsException这里我的代码我把评论放在那两个Toast旁边他们在getUserGamesInfo函数(sry发生了代码,当我在这里粘贴它时发生的事情):

public class account_games_info extends Fragment {

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

String Playerscores, Rivalscore;
String[] array = null;
ListView listView;
ArrayList<gameHistory_object> scoreList=new ArrayList<gameHistory_object>;//*************
gameHistory_object gameHistory_object;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    get_user_games_ids();//a functio that calls getUserGamesInfo but i didnt bring it here 

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View games_info = inflater.inflate(R.layout.fragment_account_games_info, container, false);
    listView = (ListView) games_info.findViewById(R.id.listView);

    return games_info;
}

private void getUserGamesInfo(String gameID) {

    final String MY_PREFS_NAME = "username and password";
    SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME,
            MODE_PRIVATE);
    final String id = prefs.getString("userID", null);

    info.put("action", "sendGameInformation");
    info.put("userID", id);
    info.put("gameID", gameID);


    JSONObject jsonObject = new JSONObject(info);
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            url, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                gameHistory_object = new gameHistory_object();
                if (id.equals(response.getString("playerOneID"))) {

                    Playerscores = response.getString("playerOneTotalScore");
                    Rivalscore = response.getString("playerTwoTotalScore");

                    gameHistory_object.setRivalUsername(response.getString("playerTwoUsername"));
                    gameHistory_object.setPlayerScore(Playerscores);
                    gameHistory_object.setRivalScore(Rivalscore);

                } else {

                    Playerscores = response.getString("playerTwoTotalScore");
                    Rivalscore = response.getString("playerTwoTotalScore");

                    gameHistory_object.setRivalUsername(response.getString("playerOneUsername"));
                    gameHistory_object.setPlayerScore(Playerscores);
                    gameHistory_object.setRivalScore(Rivalscore);

                }
                   scoreList.add(gameHistory_object);
                 //first toast which runs with no problem     

                 Toast.makeText(getActivity(),"inside $$$"+scoreList.get(0).getRivalUsername(),Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (Playerscores.equals("")) {
                playerScore_view.setText("");
            } else {
                playerScore_view.setText(Playerscores);
                rivalScore_view.setText(Rivalscore);
            }

        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

     //second toast with outofboundexecption

    Toast.makeText(getActivity(),"outside 
      $$$"+scoreList.get(0).getRivalUsername(),Toast.LENGTH_SHORT).show(); 

    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

}

2 个答案:

答案 0 :(得分:0)

您的arraylist未在代码中的任何位置初始化,因此IndexOutOfBoundsException。 在变量声明中执行此操作:

ArrayList<gameHistory_object> scoreList = new ArrayList <>();

答案 1 :(得分:0)

  1. 您永远不会初始化scoreList

    ArrayList<gameHistory_object> scoreList;
    

    应该是

    ArrayList<gameHistory_object> scoreList = new ArrayList();
    
  2. 您可以访问scoreList之前的第0个项目。您开始的JsonObjectRequest异步。您的代码将启动异步请求,然后继续到您尝试进行烘烤的部分,但请求尚未结束(onResponse部分),因此您的列表为空。