有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在开发安卓应用程序。我想获取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 安卓.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at 安卓.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) 个答案

  1. # 1 楼答案

    必须在for循环的上面声明这一行m_idlist = new ArrayList<Integer>();

  2. # 2 楼答案

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

    如果列表中可能没有索引为5的元素,请添加一个if语句以检查索引与列表大小

    就你而言:

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