当RecyclerView滚动到最后时,它会给出java.lang.ArrayIndexOutOfBoundsException:length = 9; index = 9错误。有10个链接为适配器提供图像。使用android_image_urls.length也会出现相同的错误
public class MyHorizontalAdapter extends RecyclerView.Adapter<MyHorizontalAdapter.ViewHolder> {**//line 31**
public List<Notes> dataset= new ArrayList<>();
public Context context;
private final String android_image_urls[] = {//10 image urls
};
private final String note_title[] = {
"Physics", "ElectroMagnetics", "DSA", "Electrical", "Electrical Machine", "Numerical Method"
, "Digital Logic", "EDC", "ECT"};
private final String note_author[] = {"hari", "ram", "shyam", "binod", "ghanshyam", "BP", "RP", "NPA", "IB", "TS"};
private final String download[] = {"10", "11", "23", "54", "45", "34", "32", "56", "12", "12"};
public List<Notes> add() {
for (int i = 0; i < 9; i++) {
Notes notes = new Notes(android_image_urls[i], note_title[i], note_author[i], download[i]);
dataset.add(notes);
//Toast.makeText(Most_loved.this, String.valueOf(dataset.size()), Toast.LENGTH_SHORT).show();
}
return dataset;
}
public MyHorizontalAdapter(Context context, List<Notes> dataset) {
this.context = context;
this.dataset = dataset;
}
@Override
public MyHorizontalAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.horizontal_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Picasso.with(context).load(android_image_urls[position])
.placeholder(R.drawable.ic_menu_gallery).fit().into(holder.randomImageView);**//line 79**
holder.title.setText("Title: "+ note_title[position]);
holder.author.setText("Author: " + note_author[position]);
holder.downloads.setText("Downloads: " +download[position]);
}
错误:
06-25 21:23:46.032 8498-8498/com.example.zaraki.ioenote E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zaraki.ioenote, PID: 8498
java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
at com.example.zaraki.ioenote.RecyclerView.MyHorizontalAdapter.onBindViewHolder(MyHorizontalAdapter.java:79)
at com.example.zaraki.ioenote.RecyclerView.MyHorizontalAdapter.onBindViewHolder(MyHorizontalAdapter.java:31)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:528)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1180)
at android.support.v7.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1019)
at android.support.v7.widget.GridLayoutManager.scrollHorizontallyBy(GridLayoutManager.java:361)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4057)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
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:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
答案 0 :(得分:0)
您的note_title
数组只包含9个条目,而不是10.因此索引9无效。
答案 1 :(得分:0)
从您提供的代码中,数组索引不在您控制范围内的唯一时间是方法onBindViewHolder
。
确保你传递的final int position
小于10.我怀疑你可能在某个时候打电话给array.length
而忘记减去1。