Ошибка java.lang.IndexOutOfBoundsException: Index: 0, Size: 0. Прошу помогите

Выводит ошибку: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

if(newsfeed.attachments.size() > 0) {
        for (int i = 0; i < newsfeed.attachments.size(); i++) {

            if (newsfeed.attachments.get(i).type.equals("link")) { // вот эта строка
                View to_add;
                post_audio_view.setVisibility(View.VISIBLE);

                final CardView post_link_view;
                TextView title, url;
                to_add = View.inflate(ctx, R.layout.link_post_item, null);

                post_link_view = to_add.findViewById(R.id.postLinkView);
                title = to_add.findViewById(R.id.titleText);
                url = to_add.findViewById(R.id.linkUrl);

           }
      }
 }

введите сюда описание изображения


Ответы (1 шт):

Автор решения: ArchDemon

Код лучше заменить на следующий

for (Класс attachment: newsfeed.attachments) {
    if ("link".equals(attachment.type)) { // вот эта строка
        View to_add;
        post_audio_view.setVisibility(View.VISIBLE);

        final CardView post_link_view;
        TextView title, url;
        to_add = View.inflate(ctx, R.layout.link_post_item, null);

        post_link_view = to_add.findViewById(R.id.postLinkView);
        title = to_add.findViewById(R.id.titleText);
        url = to_add.findViewById(R.id.linkUrl);
   }
}
→ Ссылка