【问题标题】:RecyclerView Crashes on Scrolldown ( IndexOutOfBoundsException: Index: 2, Size: 2 )RecyclerView 在向下滚动时崩溃(IndexOutOfBoundsException:索引:2,大小:2)
【发布时间】:2019-08-27 15:51:15
【问题描述】:

iam 从 json 解析数据并将其传递给包含两个 Arraylist 的 ArrayList,然后将它们传递给 RecyclerView ,RecyclerView 显示数据但 1 项占用整个页面,当我向下滚动时,我可以看到第二项和然后它崩溃了

我尝试了很多 ppl 提出的父母或回收商的建议 layout_heigt="wrap_content" 它甚至不会打开页面 这是回收者的视图 xml(activity_resultsdwldview) 和包含每一行的 cardview 设计的 row.xml

对不起,如果这没有组织或不清楚,我的时间真的很短,而且是一个没有太多经验的初级 android 开发人员

activity_resultsdwldview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".Resultsdwldview">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyvlerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</RelativeLayout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="12dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/nameArray"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/URLName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>


</LinearLayout>

适配器类

package com.example.imc;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {



    List<ArrayList<String>> Tests;
    ArrayList<String> urlArray;
    ArrayList<String> nameArray;

    public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> urlArray, ArrayList<String> nameArray) {
        Tests = tests;
        this.urlArray = urlArray;
        this.nameArray = nameArray;
    }



    @NonNull
    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {

        holder.URLName.setText( urlArray.get(i));
        holder.mFullname.setText( nameArray.get(i));


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView URLName;
        public TextView mFullname;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mFullname=itemView.findViewById(R.id.nameArray);
            URLName=itemView.findViewById(R.id.URLName);
        }
    }

}

这是错误日志

E/RecyclerView: No adapter attached; skipping layout
D/EGL_emulation: eglMakeCurrent: 0xa2cede60: ver 2 0 (tinfo 0xa2cefaf0)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imc, PID: 16616
    java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:40)
        at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:13)
        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.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
        at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
        at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
        at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:366)
        at android.support.v7.widget.GapWorker.run(GapWorker.java:397)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

enter code here

【问题讨论】:

    标签: java android arraylist indexoutofboundsexception cardview


    【解决方案1】:

    适配器的大小由以下人员设置:

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

    而您尝试从以下位置获取项目:

     @Override
        public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
    
            holder.URLName.setText( urlArray.get(i));
            holder.mFullname.setText( nameArray.get(i));
    
    
        }
    

    您需要将适配器大小设置为您要检索的大小:

     @Override
            public int getItemCount() {
                   urlArray.size()
            }
    

     @Override
        public int getItemCount() {
                return nameArray.sizes()
        }
    

    我建议使用单个数组,或者可能是一个地图,以避免类似的问题。

    【讨论】:

    • 如果它回答了您的问题,请随时接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2017-12-09
    • 1970-01-01
    • 2020-10-12
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多