java.lang.IndexOutOfBoundsException:索引4无效,大小为4错误

时间:2015-09-28 19:48:40

标签: android

我正在尝试运行我的项目,当我发送消息时,我收到此错误:

  

if ...
elif ...
else ...

NoteFragment:

09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime: FATAL EXCEPTION: main
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime: Process: com.clarkawilson.quarkposts, PID: 22976
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at java.util.ArrayList.get(ArrayList.java:308)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.clarkawilson.quarkposts.fragments.NotesFragment.updateMessageList(NotesFragment.java:177)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.clarkawilson.quarkposts.handler.PostHandler.processRequestResult(PostHandler.java:261)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.clarkawilson.quarkposts.handler.PostHandler.access$000(PostHandler.java:35)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.clarkawilson.quarkposts.handler.PostHandler$1.onResponse(PostHandler.java:205)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.clarkawilson.quarkposts.handler.PostHandler$1.onResponse(PostHandler.java:202)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
09-28 20:33:40.591 22976-22976/com.clarkawilson.quarkposts E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
09-28 20:33:42.161 22976-22976/com.clarkawilson.quarkposts I/Process: Sending signal. PID: 22976 SIG: 9

PostHandler:

public void updateMessageList(JSONObject jsonObject){
    try {

        /* The elements of incomingNotes are added into the noteList in reverse order,
         * i.e. treat incoming notes like a stack, so that the message we view at the
         * bottom of our adapter, is the last message received by the server.
         *
         * The second evaluation in the if statement tests whether the last element in
         * noteList is the same at the first element in the incoming list from the server.
         * If they're different, this means that new messages have been posted on the server.
         * If they're the same, nothing has changed. Don't traverse incomingList.*/

        JSONArray incomingNotes = jsonObject.getJSONArray(PostHandler.TAG_POSTS);
        NoteData nd = new NoteData(incomingNotes.getJSONObject(0));

        int incomingNotesLength = incomingNotes.length();
        if (null == noteList || !nd.equals(noteList.get(incomingNotesLength - 1))) {
            noteList = new ArrayList<>(incomingNotesLength);

            for (int i = incomingNotesLength - 1; i >= 0; i--) {
                JSONObject c = incomingNotes.getJSONObject(i);
                NoteData noteData = new NoteData(c);
                noteList.add(noteData);
            }
            updateMessageList();
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(activity,
                ERROR_GENERAL + ": " + e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

}

对此有任何帮助将不胜感激。 我确定我可能错过了一些代码,即使我检查了3到4次!

1 个答案:

答案 0 :(得分:0)

您正在尝试检索列表中仅包含4个项目的第5个项目。麻烦来自这行代码

if (null == noteList || !nd.equals(noteList.get(incomingNotesLength - 1))) {

特别是noteList.get(incomingNotesLength - 1))

这是失败的,因为incomingNotesLength已初始化为incomingNotes.length();,而不是noteList.size()

如果您要检索noteList中的最后一项,则应将此行代码更改为

if (null == noteList || !nd.equals(noteList.get(noteList.size() - 1))) {

int noteListLength = noteList.size();
if (null == noteList || !nd.equals(noteList.get(noteListLength - 1))) {