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

huangapple go评论82阅读模式
英文:

java.lang.IndexOutOfBoundsException: Index: 7, Size: 7. Why is this happening?

问题

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);
        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);
        }
    }
}
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;
    }
}

Error:

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)

The quotes.txt and authors.txt are inside the res folder of my project. Also, the app does show the first few instances of getAuthors and getQuotes but crashes after showing these. I don't know why it crashes. Maybe if I throw an exception into my adapter code to handle when there are no more items to view. But the crash happens when the authors are included in the adapter code. I removed the authors' list from being populated in the RecyclerView, and the quotes did correctly display without a crash, even if there were no more quotes.

Any help, guys?

英文:

Code for adaptor:

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&lt;quotesAdapter.quotesViewHolder&gt; {
private List&lt;String&gt; quotes;
private List&lt;String&gt; authors;
private List&lt;ImageView&gt; images = null;
private Context context;
public quotesAdapter(List&lt;String&gt; quotes, QuotesActivity quotesActivity, List&lt;String&gt; 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 = {&quot;#952222&quot;, &quot;#10171B&quot;, &quot;#FF373535&quot;, &quot;#ECE3D3&quot;, &quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;,&quot;#10171B&quot;};
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);
}
}
}

Code for 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&lt;String&gt; getQuotes() {
List&lt;String&gt; quotes = new ArrayList&lt;&gt;();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open(&quot;quotes.txt&quot;), &quot;UTF-8&quot;));
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&lt;String&gt; getAuthors() {
List&lt;String&gt; authors = new ArrayList&lt;&gt;();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open(&quot;authors.txt&quot;), &quot;UTF-8&quot;));
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;
}
}

Error:

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)

The quotes.txt and authors.txt are inside the res folder of my project. Also, the app does show the first few instances of getAuthors and getQuotes but crashes after showing these. I dont know why it crashes. Maybe if i throw an exception into my adaptor code to handle when there is no more items to view. But the crash happens when the authors is included in the adaptor code. I removed the authors list from being populated in the recyclerview and the quotes did correctly display without crash, even if there was nothing left to display (no more quotes).

Any help guys?

答案1

得分: 0

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

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

错误信息

java.lang.IndexOutOfBoundsException: Index: 7, Size: 7

表示

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

英文:

Index begins from 0. Therefore, for an array of size 7, the index will be from 0 to 6.

  • If the index is 0, then it refers to the 1st element of the array.
  • If the index is 1, then it refers to the 2nd element.
  • ...

The error

java.lang.IndexOutOfBoundsException: Index: 7, Size: 7

signifies that

The array has only 7 elements, but an attempt was made to extract the 8th element.

huangapple
  • 本文由 发表于 2020年4月11日 03:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61146999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定