java.lang.IndexOutOfBoundsException:无效的索引2,大小为2,依此类推

时间:2018-07-15 13:26:13

标签: java android json

我有很多微调器(总共4个),其中包含json的列表。和微调器之后,它会根据我在微调器上选择的内容进行填充。

这是我的将json发送到微调器的代码(例如,我只放置了2个微调器)

 class daftarGedung extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(TambahLaporActivity.this);
            pDialog.setMessage("Mengambil Data...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            link_url = "https://example.com/api/assets?parent=0";
            HttpHandler sh = new HttpHandler();
            String json = sh.makeServiceCall(link_url);
            final boolean ambilspn = gedungSpn.getSelectedItem() == tidakada;

            Log.e(TAG, "Response from url: " + json);
            if (json != null) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject ar = data.getJSONObject(i);
                        gedung = ar.getString("nama");
                        id_gedung_int = ar.getInt("id");
                        id_gedung1 = ar.getString("id");
                        Log.d(TAG, String.valueOf(id_gedung_int));

                        list.add(gedung);
                        list_id.add(id_gedung1);

                    }

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

            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    data_adapter_gedung.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    gedungSpn.setAdapter(data_adapter_gedung);
                    Log.e("isi gedung", "" + ambilspn);
                    if (ambilspn == false) {
                        gedungSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                            if (gedungSpn.getSelectedItem() != tidakada) {
                                id_gedung_int = list.indexOf(gedungSpn.getSelectedItem());
                                id_gedung = list_id.get(id_gedung_int);
                                list2.clear();
                                list2_id.clear();
                                new daftarLantai().execute();

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {
                                komen = "not_selected";
                            }
                        });
                    }

                }
            });
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }
    }

    class daftarLantai extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(TambahLaporActivity.this);
            pDialog.setMessage("Mengambil Data...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            link_url = "https://monasdev.example.com/api/assets?parent=" + id_gedung;
            HttpHandler sh = new HttpHandler();
            String json = sh.makeServiceCall(link_url);
            final boolean ambilspn = gedungSpn.getSelectedItem() == tidakada;
            list2.add(tidakada);

            Log.e(TAG, "Response from url: " + json);
            if (json != null) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject ar = data.getJSONObject(i);
                        lantai = ar.getString("nama");
                        id_lantai1 = ar.getString("id");

                        if (ambilspn == false) {
                            list2.add(lantai);
                            list2_id.add(id_lantai1);
                        } else {
                            list2.add(tidakada);
                            list3.add(tidakada);
                            list4.add(tidakada);
                        }
                    }

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

            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {


                    data_adapter_lantai.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//                    
                    lantaiSpn.setAdapter(data_adapter_lantai);
                    data_adapter_ruang.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    ruangSpn.setAdapter(data_adapter_ruang);
                    data_adapter_aset.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    asetSpn.setAdapter(data_adapter_aset);
//                    Log.e("isi",""+ambilspn);
                    if (ambilspn == false) {
                        lantaiSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                id_lantai_int = list2.indexOf(lantaiSpn.getSelectedItem());
                                id_lantai = list2_id.get(id_lantai_int);
                                list3.clear();
                                list3_id.clear();

                                new daftarRuangan().execute();

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {
                                komen = "not_selected";
                            }
                        });
//                    
                    } else {
                        list2.add(tidakada);
                        list2_id.add(id_lantai1);
                    }
                }
            });
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }
    }

有时有效,有时无效。例如,当我选择第一个微调器的第二个列表时,它将显示错误java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2。并可能是错误java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5等,具体取决于导致应用崩溃的微调器。但是错误总是相同,只是索引(数字)不同。

这是我得到的全部错误

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
                         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                         at java.util.ArrayList.get(ArrayList.java:308)
                         at com.mqa.android.monas.Activity.TambahLaporActivity$daftarGedung$1$1.onItemSelected(TambahLaporActivity.java:647)
                         at android.widget.AdapterView.fireOnSelected(AdapterView.java:914)
                         at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:903)
                         at android.widget.AdapterView.access$300(AdapterView.java:48)
                         at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:873)
                         at android.os.Handler.handleCallback(Handler.java:739)
                         at android.os.Handler.dispatchMessage(Handler.java:95)
                         at android.os.Looper.loop(Looper.java:135)
                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at java.lang.reflect.Method.invoke(Method.java:372)
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

请帮助我的代码有什么问题

2 个答案:

答案 0 :(得分:0)

我没有问题,但是尝试一下并告诉我它是否有效

 id_lantai = list2_id.get(id_lantai_int - 1);

答案 1 :(得分:0)

您似乎没有使list2list2_id保持同步。您可以在许多地方将项目添加到list2而不是list2_id上,因此在运行时

id_lantai_int = list2.indexOf(lantaiSpn.getSelectedItem());
id_lantai = list2_id.get(id_lantai_int);

您可以获得IndexOutOfBoundsException。假设您在list2中有10个项目,但是在list2_id中只有4个项目,那么id_lantai_int的范围可以是0到9。

使持有的list2具有id属性比尝试使两个列表保持同步要更健壮,但是尚不清楚仅根据您的代码看起来会如何已发布。

此外,要获取显示列表中所选项目的索引,您只需将第三个参数(int i)用作onItemSelected