java.lang.IndexOutOfBoundsException : Index: 7, 大小 : 7. 为什么会发生这种情况?

标签 java android android-recyclerview android-adapter indexoutofboundsexception

适配器代码:

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class quotesAdapter extends RecyclerView.Adapter<quotesAdapter.quotesViewHolder> {

    private List<String> quotes;
    private List<String> authors;
    private List<ImageView> images = null;
    private Context context;

    public quotesAdapter(List<String> quotes, QuotesActivity quotesActivity, List<String> authors, Context context) {
        this.quotes = quotes;
        this.authors = authors;
        this.context = context;
    }


    @NonNull
    @Override
    public quotesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.example_item_quotes, viewGroup, false);

//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_quotes, parent, false);
//        RecyclerView.ViewHolder holder = new RecyclerView.ViewHolder(view) {

        return new quotesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull quotesViewHolder holder, int i) {
        String[] colors = {"#952222", "#10171B", "#FF373535", "#ECE3D3", "#10171B","#10171B","#10171B","#10171B","#10171B","#10171B","#10171B","#10171B","#10171B"};
        String quote = quotes.get(i);
        ***String author = authors.get(i);***
        holder.txtQuote.setText(quote);
        holder.txtAuthor.setText(author);
        int color = i % colors.length;
        int intColor = Color.parseColor(colors[color]);
        holder.quoteContainer.setBackgroundColor(intColor);
        holder.txtQuote.setBackgroundColor(intColor);
        holder.txtAuthor.setBackgroundColor(intColor);

    }

    @Override
    public int getItemCount() {
        return quotes.size();
    }

    public static class quotesViewHolder extends RecyclerView.ViewHolder {

        TextView txtQuote;
        TextView txtAuthor;
        LinearLayout quoteContainer;

        public quotesViewHolder(@NonNull View itemView) {
            super(itemView);
            txtQuote = itemView.findViewById(R.id.txtQuote);
            txtAuthor = itemView.findViewById(R.id.txtAuthor);
            quoteContainer = itemView.findViewById(R.id.quotesContainer);
        }
    }
}

Activity 代码:

import android.media.Image;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class QuotesActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quotes_act);
        RecyclerView quotesList = findViewById(R.id.quotesList);
        quotesList.setLayoutManager(new LinearLayoutManager(this));
        quotesList.setAdapter(new quotesAdapter(getQuotes(), this,getAuthors(),this));
    }

    private List<String> getQuotes() {
        List<String> quotes = new ArrayList<>();
        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open("quotes.txt"), "UTF-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                quotes.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return quotes;
    }

    private List<String> getAuthors() {
        List<String> authors = new ArrayList<>();
        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open("authors.txt"), "UTF-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                authors.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return authors;
    }

}

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: konak.labs.warstoriesv2, PID: 820
    java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
        at java.util.ArrayList.get(ArrayList.java:437)
        at konak.labs.warstoriesv2.quotesAdapter.onBindViewHolder(quotesAdapter.java:47)
        at konak.labs.warstoriesv2.quotesAdapter.onBindViewHolder(quotesAdapter.java:17)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
        at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
        at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
        at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:366)
        at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:397)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

quotes.txt 和authors.txt 位于我项目的res 文件夹内。此外,该应用程序确实显示了 getAuthors 和 getQuotes 的前几个实例,但在显示这些实例后崩溃了。我不知道为什么它崩溃了。也许如果我在适配器代码中抛出一个异常来处理没有更多项目可查看的情况。但是当作者包含在适配器代码中时,就会发生崩溃。我从 recyclerview 中删除了作者列表,并且引号正确显示而不会崩溃,即使没有任何内容可显示(不再有引号)。

大家有什么帮助吗?

最佳答案

索引从 0 开始。因此,对于大小为 7 的数组,索引将从 0 到 6。

  • 如果索引为 0,则它指的是数组的第一个元素
  • 如果索引为1,则它引用第二个元素
  • ...

错误

java.lang.IndexOutOfBoundsException:索引:7,大小:7

表示

数组只有 7 个元素,但尝试提取第 8 个元素。

关于java.lang.IndexOutOfBoundsException : Index: 7, 大小 : 7. 为什么会发生这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61146999/

相关文章:

Java反射-枚举生成

java - 使用 owl api 获取单个属性

android - RecyclerView 的第一项丢失

android - 在 RecyclerView 上插入新项目

java - 什么是NullPointerException,我该如何解决?

java - Websocket 一段时间后(20-30 分钟)停止接收数据

android - 关于 NotifyDataSetChanged

android - AchartEngine 图表值颜色

Android 蓝牙扫描已配对

android - 用刀柄注入(inject) viewModel