简体   繁体   English

Android arraylist崩溃与迭代

[英]Android arraylist crashes with iteration

I've been trying to make a multiplayer, turn-based app, and so far things have been going smoothly: My app can communicate with a local server, and retrieve information from it. 我一直在尝试制作多人,回合制应用程序,到目前为止,一切进展顺利:我的应用程序可以与本地服务器通信,并从中获取信息。 Now when I try to put that information I recieved into an arraylist, I get no errors, but the arraylist stays empty, which causes my app to crash when I call myArrayList.get(i). 现在,当我尝试将收到的信息放入arraylist时,没有任何错误,但是arraylist保持为空,这在调用myArrayList.get(i)时导致我的应用程序崩溃。 Here is the code that sets the array: 这是设置数组的代码:
I BELIEVE THE PROBLEM IS SETTING THE ARRAY BELOW 我相信问题是在下面设置数组

//The "i" below is the iterator used for the index
        for (int i = 1; i < getSharedPreferences("INFO", MODE_PRIVATE).getInt("numberOfGames", 0) + 1; i++) {
            try {
                SharedPreferences s = getSharedPreferences("INFO", MODE_PRIVATE);
                SharedPreferences.Editor e = s.edit();
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("gameid", s.getString("gameID" + i, null)));
                Log.d("request!", "starting");
                JSONObject json = jsonParser.makeHttpRequest(
                        REFRESH_URL, "POST", params);
                success = json.getInt("success");
                Log.i("LOGI", "Success: " + success);
                if (success == 1) {
                    Log.i("LOGI", "**********GAME #" + s.getString("gameID" + i, null) + "**********");
                    Log.i("LOGI", "PLAYER1: " + json.getString("player1"));
                    Log.i("LOGI", "PLAYER2: " + json.getString("player2"));
                    Log.i("LOGI", "Score1: " + json.getString("score1"));
                    Log.i("LOGI", "Score2: " + json.getString("score2"));
                    Log.i("LOGI", "Turn: " + json.getString("turn"));
                    Log.i("LOGI", "Game Chosen: " + json.getString("gameChosen"));

                    //EVERYING BELOW IS THE PROBLEM
                    player1.add(i, json.getString("player1"));
                    player2.add(i, json.getString("player2"));
                    score1.add(i, Integer.parseInt(json.getString("score1")));
                    score2.add(i, Integer.parseInt(json.getString("score2")));
                    turn.add(i, Integer.parseInt(json.getString("turn")));
                    gameChosen.add(i, Integer.parseInt(json.getString("gameChosen")));
                }

Here is the code that gets the array 这是获取数组的代码

private void drawCurrentGames(Canvas canvas){
    String player1;
    String player2;
    String opponent;
    int score1;
    int score2;
    Log.i("LOGI", "WOERKIGN");
    for(int i=0; i<Main.games; i++){
        //The app crashes here
        player1 = Main.player1.get(i);
        player2 = Main.player2.get(i);
        score1 = Main.score1.get(i);
        score2 = Main.score2.get(i);
    }
}

When an iterator is not used to set or get the arraylist, everything is fine, but when it is the app crashes 当不使用迭代器来设置或获取arraylist时,一切都很好,但是当应用程序崩溃时

Your iterator that adds items to the array starts at 1. When you call the add(int, Object) method of an arraylist, the object being added is added at the index of the first parameter. 将项目添加到数组的迭代器从1开始。当您调用arraylist的add(int, Object)方法时,要添加的对象将添加到第一个参数的索引处。

So the first item to be added is being added at index 1. When you go to read from that array list, you're starting at 0. There's nothing at index 0, which is why your app is crashing. 因此,要添加的第一项是在索引1处添加的。当您从该数组列表中进行读取时,您将从0开始。索引0处没有任何内容,这就是您的应用程序崩溃的原因。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM