java.lang.IndexOutOfBoundsException:索引5无效,大小为1

时间:2017-04-20 10:13:45

标签: java android arraylist android-asynctask indexoutofboundsexception

我正在开发Android应用程序。我想得到id的特定索引,但它总是返回索引id或应用程序崩溃的第一个。请帮助我如何解决错误。感谢

这是我的代码:

 protected Void doInBackground(Void... arg0)
    {
        HttpService httpService = new HttpService("http://192.168.0.103/GetActiveGroupMember.php");
        try
        {
            httpService.ExecutePostRequest();

            if(httpService.getResponseCode() == 200)
            {
                result = httpService.getResponse();
                ArrayList<Integer> m_idlist = null;

                listid = new ArrayList<>();
                listid.add(result);
               // System.out.println("ListID...  "+listid+" ListId Size "+listid.size());
                System.out.println("ListID...  "+listid+" ListId Size "+listid.size());
             //   Log.d("Result", result);
                System.out.println("Result "+result);
                if(result != null)
                {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(result);

                        JSONObject object;
                        JSONArray array;

                        member m;
                        members = new ArrayList<member>();

                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            m= new member();
                            object = jsonArray.getJSONObject(i);

                          //  m.username = object.getString("username");
                            m.id = object.getInt("id");

                            members.add(m);

                            int m_id  = m.id;
                            System.out.println("mem_id  = "+m_id);

                            m_idlist = new ArrayList<Integer>();
                            m_idlist.add(i);

                          System.out.println("five no id = "+m_idlist.get(i)); // TO print all Id

                        }
                         if(m_idlist.size()>5){
                            System.out.println("five no id = "+m_idlist.get(5));
                           }
                     //   System.out.println("Total member  = "+members.size());

                    }

错误:

W/System.err: java.lang.IndexOutOfBoundsException: Invalid index 5, size is 1
W/System.err:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
W/System.err:     at java.util.ArrayList.get(ArrayList.java:308)
W/System.err:     at com.example.moraya.adminmodule.GetActiveGroupMemberACtivity$GetHttpResponse.doInBackground(GetActiveGroupMemberACtivity.java:121)
W/System.err:     at com.example.moraya.adminmodule.GetActiveGroupMemberACtivity$GetHttpResponse.doInBackground(GetActiveGroupMemberACtivity.java:56)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:841)
W/EGL_genymotion: eglSurfaceAttrib not implemented

非常感谢你。

2 个答案:

答案 0 :(得分:0)

System.out.println("five no id = "+m_idlist.get(5));

列表中可能没有索引为5的元素,添加if语句以检查索引的大小。

在你的情况下:

if (5 < m_idlist.size()) { ... }

答案 1 :(得分:0)

您必须在for循环

之上声明此行m_idlist = new ArrayList<Integer>();