java.lang.IndexOutOfBoundsException:当我开始向下滚动应用程序时,抛出了检测到的不一致

时间:2019-06-24 18:56:07

标签: java android android-cardview

我有一个cardview,我在其中根据是否满足特定条件来动态加载textview。从下面的代码中可以看到,onBindViewHolder中有2个if条件。如果满足,第一个将始终产生1个textview。所以我有1张卡,其值来自mPurchaseDateList,一张值来自mVehicleList。这样可以产生具有该结构的多个卡。我的第二个条件将始终产生1张卡,该卡具有mPurchaseDateList中的值,而mVehicleList中具有MULTIPLE个值,这取决于“昨天”的出现次数。列表中值的位置已经按照我的需要进行了排序,并且两个列表中的计数始终相等。我用下面的代码实现了我所需要的,但是当我向下滚动时,我总是遇到错误。我认为这是因为我添加数据后会从列表中删除数据,以避免重复。

public DataAdapter(MainActivity activity, ArrayList<String> mVehicleList, ArrayList<String> mPurchaseDateList) {
    this.mActivity = activity;
    this.mVehicleList= mVehicleList;
    this.mPurchaseDateList= mPurchaseDateList;
    countPurchaseDateList();
}

    private LinearLayout llVehicleName;
    private TextView tv_PurchaseDate;


    public MyViewHolder(View view) {
        super(view);
        llVehicleName = (LinearLayout) view.findViewById(R.id.row_tv_vehicle_name);
        tv_PurchaseDate = (TextView) view.findViewById(R.id.row_tv_purchase_date);
    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_row_data, parent, false);

    return new MyViewHolder(itemView);
}
 @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        //the header
        holder.tv_PurchaseDate.setText(mPurchaseDateList.get(position));

            if (mPurchaseDateList.get(position).equalsIgnoreCase("Today")) {

                addTextViews(holder, position);

            }
            if (mPurchaseDateList.get(position).equalsIgnoreCase("Yesterday")) {

                for(int i=0;i < yesterdayCounter;i++) {

                    addTextViews(holder, position);

                    //remove the vehicle and date to avoid duplicates
                    mVehicleList.remove(position);
                    mPurchaseDateList.remove(position);
                }

            }

    }

以下方法添加了我的文本视图(或多个实例,具体取决于"Yesterday"中出现mPurchaseDateList的次数)

   private void addTextViews(MyViewHolder holder, final int position) {

        RecyclerView.LayoutParams lparams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
        TextView tv = new TextView(holder.llVehicleName.getContext());
        tv.setLayoutParams(lparams);
        tv.setText(mVehicleList.get(position));
        holder.llVehicleName.addView(tv);

    }

以下方法告诉我"Yesterday"出现了多少次:

public void countPurchaseDateList () {
    for(int i=0; i < mPurchaseDateList.size(); i++)
    if (mPurchaseDateList.get(i).equalsIgnoreCase("Yesterday")) {
        yesterdayCounter++;
    }
}

开始向下滚动后引发的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mySampleProject, PID: 11262
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{c91c8a2 position=7 id=-1, oldPos=-1, pLpos:-1 no parent} android.support.v7.widget.RecyclerView{47ff933 VFED..... ........ 0,0-1440,2432 #7f070006 app:id/act_recyclerview}, adapter:com.mySampleProject.DataAdapter@8dcd4f0, layout:android.support.v7.widget.LinearLayoutManager@87fa69, context:com.mySampleProject.Activity.MainActivity@d943958
        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
        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.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1331)
        at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1075)
        at android.support.v7.widget.RecyclerView.scrollStep(RecyclerView.java:1832)
        at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1927)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3187)
        at android.view.View.dispatchTouchEvent(View.java:12513)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2705)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:440)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1830)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3400)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:398)
        at android.view.View.dispatchPointerEvent(View.java:12752)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5106)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4909)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4642)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7092)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7061)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7022)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7195)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:177)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7166)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7218)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:690)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        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:6669)
        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:858)

0 个答案:

没有答案