java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder
大家下午好,我在 recyclerview 中创建列表时遇到了这个问题。
我的 API 响应有一个列表,在该列表中有一个图像列表,我需要访问它才能获取图像 link 并将其上传到我的图像视图。
我的 API 回复:
{
"data": [
{
"id": "KCLSwV5",
"title": "I wake up when i have my morning coffee",
"description": null,
"images": [
{
"id": "k47zXXl",
"title": null,
"link": "https://i.imgur.com/k47zXXl.jpg"
}
]
}
]
}
所以我这样创建了我的模型。
public class Data implements Serializable {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("images")
@Expose
private List<Images> images = null;
//Getters and Setters
public List<Images> getImages() { return images; }
public void setImages(List<Images> images) { this.images = images; }
错误记录:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.allan.galleryapp.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:39)
at com.example.allan.galleryapp.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:23187)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:23187)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:641)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:755)
at android.view.View.measure(View.java:23187)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2757)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1606)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1890)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1494)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7288)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
日志显示我的适配器在 onBindViewHolder 方法中发生错误。
我的适配器:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private Context context;
private List<Data> dataList;
public ListAdapter(Context context, List<Data> dataList) {
this.context = context;
this.dataList = dataList;
}
@NonNull
@Override
public ListAdapter.ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_item, viewGroup, false);
return new ListAdapter.ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ListViewHolder listViewHolder, int i) {
String image = dataList.get(i).getImages().get(i).getLink(); //LINE 39 I believe the error is here, but I don't know how to solve it.
Glide.with(context)
.load(image)
.apply(new RequestOptions()
.placeholder(R.drawable.load))
.into(listViewHolder.imageView);
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ListViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
}
}
}
如果有人能帮助我,我将不胜感激。
干杯。
解析错误使用此模型,使用本网站制作模型。
Jsontopojo
public class Example {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("images")
@Expose
private List<Image> images = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
}
-----------------------------------com.example.Image.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private Object title;
@SerializedName("link")
@Expose
private String link;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Object getTitle() {
return title;
}
public void setTitle(Object title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
大家下午好,我在 recyclerview 中创建列表时遇到了这个问题。 我的 API 响应有一个列表,在该列表中有一个图像列表,我需要访问它才能获取图像 link 并将其上传到我的图像视图。
我的 API 回复:
{
"data": [
{
"id": "KCLSwV5",
"title": "I wake up when i have my morning coffee",
"description": null,
"images": [
{
"id": "k47zXXl",
"title": null,
"link": "https://i.imgur.com/k47zXXl.jpg"
}
]
}
]
}
所以我这样创建了我的模型。
public class Data implements Serializable {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("images")
@Expose
private List<Images> images = null;
//Getters and Setters
public List<Images> getImages() { return images; }
public void setImages(List<Images> images) { this.images = images; }
错误记录:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.allan.galleryapp.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:39)
at com.example.allan.galleryapp.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:23187)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:23187)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:641)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6752)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:755)
at android.view.View.measure(View.java:23187)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2757)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1606)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1890)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1494)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7288)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
日志显示我的适配器在 onBindViewHolder 方法中发生错误。
我的适配器:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private Context context;
private List<Data> dataList;
public ListAdapter(Context context, List<Data> dataList) {
this.context = context;
this.dataList = dataList;
}
@NonNull
@Override
public ListAdapter.ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_item, viewGroup, false);
return new ListAdapter.ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ListViewHolder listViewHolder, int i) {
String image = dataList.get(i).getImages().get(i).getLink(); //LINE 39 I believe the error is here, but I don't know how to solve it.
Glide.with(context)
.load(image)
.apply(new RequestOptions()
.placeholder(R.drawable.load))
.into(listViewHolder.imageView);
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ListViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
}
}
}
如果有人能帮助我,我将不胜感激。
干杯。
解析错误使用此模型,使用本网站制作模型。 Jsontopojo
public class Example {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("images")
@Expose
private List<Image> images = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
}
-----------------------------------com.example.Image.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private Object title;
@SerializedName("link")
@Expose
private String link;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Object getTitle() {
return title;
}
public void setTitle(Object title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}