我如何修复java.lang.IndexOutOfBoundsException:Android上的ArrayList中的索引无效

时间:2017-09-16 14:09:59

标签: android list arraylist indexoutofboundsexception

在我的应用程序中,我想从服务器获取一些列表,并将此列表显示为 ChipCloud ChipCloud标记库。 图书馆链接: https://github.com/adroitandroid/ChipCloud
。我写下面的代码但是当点击项目时运行应用程序会显示在错误

下面
java.lang.IndexOutOfBoundsException: Invalid index 11, size is 6
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.com.Activities.FullSearchMini$2$5.chipSelected(FullSearchMini.java:286)
at com.adroitandroid.chipcloud.ChipCloud.chipSelected(ChipCloud.java:253)
at com.adroitandroid.chipcloud.Chip.onClick(Chip.java:138)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
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:5349)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

我的代码:

public class FullSearchMini extends AppCompatActivity {

@BindView(R.id.miniFullSearch_toolbarText)
EditText fullSearchMini_headerText;
@BindView(R.id.fullSearchMini_celebritiesRecyclerView)
RecyclerView fullSearchMini_celebritiesRecyclerView;
@BindView(R.id.miniFullSearch_movieRecyclerView)
RecyclerView miniFullSearch_movieRecyclerView;
@BindView(R.id.miniFullSearch_SeriesRecyclerView)
RecyclerView miniFullSearch_SeriesRecyclerView;
@BindView(R.id.miniFullSearch_UserRecyclerView)
RecyclerView miniFullSearch_UserRecyclerView;
@BindViews({R.id.fullSearchMini_celebritiesHeaderLay, R.id.miniFullSearch_movieHeaderLay, R.id.miniFullSearch_SeriesHeaderLay,
        R.id.miniFullSearch_UserHeaderLay})
List<RelativeLayout> miniSearchLayouts;
@BindViews({R.id.celebritiesLine, R.id.moviesLine, R.id.SeriesLine})
List<RelativeLayout> miniSearchLines;
@BindView(R.id.fullSearchMini_didYouMeanLay)
RelativeLayout fullSearchMini_didYouMeanLay;
@BindView(R.id.miniFullSearch_LoadLay)
RelativeLayout miniFullSearch_LoadLay;
@BindView(R.id.fullSearchMini_chipCloud)
ChipCloud fullSearchMini_chipCloud;
@BindView(R.id.fullSearchMini_EmptyLay)
RelativeLayout fullSearchMini_EmptyLay;
@BindView(R.id.empty_text)
TextView empty_text;
@BindView(R.id.miniFullSearch_LoadProgress)
ProgressBar miniFullSearch_LoadProgress;
private Context context;
private MiniSearchCelebritiesAdapter celebritiesAdapter;
private MiniSearchMoviesAdapter moviesAdapter;
private MiniSearchSeriesAdapter seriesAdapter;
private MiniSearchUsersAdapter userAdapter;
private List<Celebrity> celebritiesModel = new ArrayList<>();
private List<Movie> moviesModel = new ArrayList<>();
private List<Series> seriesModel = new ArrayList<>();
private List<User> userModel = new ArrayList<>();
private String searchKey, chipKey;
private List<String> cloudChipList = new ArrayList<>();
private String[] mostlyMatchedKeywordsStrings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mini_full_search);

    // Initialize
    ButterKnife.bind(this);
    context = this;

    //Set Color to progressBar
    miniFullSearch_LoadProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#ff8d00"),
            android.graphics.PorterDuff.Mode.SRC_ATOP);

    celebritiesAdapter = new MiniSearchCelebritiesAdapter(context, celebritiesModel);
    moviesAdapter = new MiniSearchMoviesAdapter(context, moviesModel);
    seriesAdapter = new MiniSearchSeriesAdapter(context, seriesModel);
    userAdapter = new MiniSearchUsersAdapter(context, userModel);

    initRecyclerView(fullSearchMini_celebritiesRecyclerView);
    initRecyclerView(miniFullSearch_movieRecyclerView);
    initRecyclerView(miniFullSearch_SeriesRecyclerView);
    initRecyclerView(miniFullSearch_SeriesRecyclerView);
    initRecyclerView(miniFullSearch_UserRecyclerView);

    // TextWatcher
    fullSearchMini_headerText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            searchKey = editable.toString();
            if (searchKey.length() > 1) {
                getData(searchKey);
            }
        }
    });
}

private void getData(String key) {
    FullSearchSendData sendData = new FullSearchSendData();
    sendData.setKey(key);
    sendData.setLoadImages(true);
    sendData.setSearchInCelebrities(true);
    sendData.setSearchInMovies(true);
    sendData.setSearchInSeries(true);
    sendData.setSearchInEpisodes(false);
    sendData.setSearchInUsers(true);
    sendData.setPageIndex(1);
    sendData.setPageSize(4);
    sendData.setMaxDistance(1);

    miniFullSearch_LoadLay.setVisibility(View.VISIBLE);

    InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
    Call<FullSearchResponse> call = api.getFullSearch(sendData);

    call.enqueue(new Callback<FullSearchResponse>() {
        @Override
        public void onResponse(Call<FullSearchResponse> call, Response<FullSearchResponse> response) {
            FullSearchResponse searchResponse = response.body();
            if (searchResponse.getData().getCelebrities() != null) {
                if (searchResponse.getData().getCelebritiesCount() > 0) {
                    celebritiesModel.clear();
                    celebritiesModel.addAll(searchResponse.getData().getCelebrities());
                    celebritiesAdapter.notifyDataSetChanged();
                    fullSearchMini_celebritiesRecyclerView.setAdapter(celebritiesAdapter);
                    miniSearchLayouts.get(0).setVisibility(View.VISIBLE);
                    miniSearchLines.get(0).setVisibility(View.VISIBLE);
                    fullSearchMini_celebritiesRecyclerView.setVisibility(View.VISIBLE);

                    miniSearchLayouts.get(0).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, FullSearch.class);
                            intent.putExtra(ExtraContains.FULL_SEARCH_TEXT.name(), searchKey);
                            startActivity(intent);
                        }
                    });
                } else {
                    miniSearchLayouts.get(0).setVisibility(View.GONE);
                    miniSearchLines.get(0).setVisibility(View.GONE);
                    fullSearchMini_celebritiesRecyclerView.setVisibility(View.GONE);
                }
            }
            if (searchResponse.getData().getMovies() != null) {
                if (searchResponse.getData().getMoviesCount() > 0) {
                    moviesModel.clear();
                    moviesModel.addAll(searchResponse.getData().getMovies());
                    moviesAdapter.notifyDataSetChanged();
                    miniFullSearch_movieRecyclerView.setAdapter(moviesAdapter);
                    miniSearchLayouts.get(1).setVisibility(View.VISIBLE);
                    miniSearchLines.get(1).setVisibility(View.VISIBLE);
                    miniFullSearch_movieRecyclerView.setVisibility(View.VISIBLE);

                    miniSearchLayouts.get(1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, FullSearch.class);
                            intent.putExtra(ExtraContains.FULL_SEARCH_TEXT.name(), searchKey);
                            startActivity(intent);
                        }
                    });
                } else {
                    miniSearchLayouts.get(1).setVisibility(View.GONE);
                    miniSearchLines.get(1).setVisibility(View.GONE);
                    miniFullSearch_movieRecyclerView.setVisibility(View.GONE);
                }
            }
            if (searchResponse.getData().getSeries() != null) {
                if (searchResponse.getData().getSeriesCount() > 0) {
                    seriesModel.clear();
                    seriesModel.addAll(searchResponse.getData().getSeries());
                    seriesAdapter.notifyDataSetChanged();
                    miniFullSearch_SeriesRecyclerView.setAdapter(seriesAdapter);
                    miniSearchLayouts.get(2).setVisibility(View.VISIBLE);
                    miniSearchLines.get(2).setVisibility(View.VISIBLE);
                    miniFullSearch_SeriesRecyclerView.setVisibility(View.VISIBLE);

                    miniSearchLayouts.get(2).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, FullSearch.class);
                            intent.putExtra(ExtraContains.FULL_SEARCH_TEXT.name(), searchKey);
                            startActivity(intent);
                        }
                    });
                } else {
                    miniSearchLayouts.get(2).setVisibility(View.GONE);
                    miniSearchLines.get(2).setVisibility(View.GONE);
                    miniFullSearch_SeriesRecyclerView.setVisibility(View.GONE);
                }
            }
            if (searchResponse.getData().getUsers() != null) {
                if (searchResponse.getData().getUsersCount() > 0) {
                    userModel.clear();
                    userModel.addAll(searchResponse.getData().getUsers());
                    userAdapter.notifyDataSetChanged();
                    miniFullSearch_UserRecyclerView.setAdapter(userAdapter);
                    miniSearchLayouts.get(3).setVisibility(View.VISIBLE);
                    miniFullSearch_UserRecyclerView.setVisibility(View.VISIBLE);

                    miniSearchLayouts.get(3).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, FullSearch.class);
                            intent.putExtra(ExtraContains.FULL_SEARCH_TEXT.name(), searchKey);
                            startActivity(intent);
                        }
                    });
                } else {
                    miniSearchLayouts.get(3).setVisibility(View.GONE);
                    miniFullSearch_UserRecyclerView.setVisibility(View.GONE);
                }
            }

            // Did you mean
            String[] mostlyMatchedKeywordsStrings = searchResponse.getData().getMostlyMatchedKeywordsText();
            cloudChipList.clear();
            for (int i = 0; i < mostlyMatchedKeywordsStrings.length; i++) {
                cloudChipList.add(mostlyMatchedKeywordsStrings[i]);
                if (i >= mostlyMatchedKeywordsStrings.length - 2) {
                    fullSearchMini_didYouMeanLay.setVisibility(View.VISIBLE);
                    fullSearchMini_chipCloud.addChip(mostlyMatchedKeywordsStrings[i]);

                    Log.e("searchKeys", mostlyMatchedKeywordsStrings[i]);
                }
            }

            fullSearchMini_chipCloud.setChipListener(new ChipListener() {
                @Override
                public void chipSelected(int i) {
                    chipKey = cloudChipList.get(i);
                    Intent intent = new Intent(context, FullSearch.class);
                    intent.putExtra(ExtraContains.FULL_SEARCH_TEXT.name(), chipKey);
                    startActivity(intent);

                }

                @Override
                public void chipDeselected(int i) {

                }
            });

            if (searchResponse.getData().getCelebrities().size() == 0 && searchResponse.getData().getSeries().size() == 0
                    && searchResponse.getData().getMovies().size() == 0 && searchResponse.getData().getUsers().size() == 0) {
                fullSearchMini_EmptyLay.setVisibility(View.VISIBLE);
                empty_text.setText(context.getResources().getString(R.string.noResultFound) + " for : " + searchKey);
            } else {
                fullSearchMini_EmptyLay.setVisibility(View.GONE);
            }

            miniFullSearch_LoadLay.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<FullSearchResponse> call, Throwable t) {
            miniFullSearch_LoadLay.setVisibility(View.GONE);
        }
    });
}

显示此行的错误:

chipKey = cloudChipList.get(i);

如何修复此错误?请帮帮我我的朋友。我真的需要你的帮助。请

2 个答案:

答案 0 :(得分:0)

在错误中说,

cloudChipList

这意味着searchResponse.getData().getMostlyMatchedKeywordsText();中的实际元素数仅为6.也就是说,public void chipSelected(int i)函数只返回6个值,然后存储在数组列表中。但是,当您调用i函数时,如果java.lang.IndexOutOfBoundsException的值大于5,则会显示此i。如果您的数组列表中有6个值,那么您只能提供从0到5的索引值。在您的情况下,public void chipSelected(int i)中{{1}}的值为11,因此错误。

答案 1 :(得分:0)

schipSelected方法的索引正在ChipCloud类中计算。

fullSearchMini_chipCloud.setChipListener(new ChipListener() {
            @Override
            public void chipSelected(int i) {  //This is callback from ChipCloud class
                chipKey = cloudChipList.get(i);
            }

            @Override
            public void chipDeselected(int i) {

            }
        });

fullSearchMini_chipCloud布局正在添加重复筹码。在添加新筹码之前,您需要清除现有的孩子。

清除列表后
cloudChipList.clear();

使用

删除现有的子项
fullSearchMini_chipCloud.removeAllViews()