Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException is occuring

Tags:

java

android

I coded one handler to maintain the auto-scroll of a recyclerview. it's working fine. but the issue I am faced when there is only one item in the recyclerview. my app got crashed and when I checked logcat I am getting an error like java.lang.IllegalArgumentException: Invalid target position.

Here is my Custom LinearLayoutManager Class

public class CustomLinearLayoutManager extends LinearLayoutManager {

    public CustomLinearLayoutManager (Context context) {
        super(context);
    }

    public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        final LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    private static final float MILLISECONDS_PER_INCH = 100f;

                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return CustomLinearLayoutManager.this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    @Override
                    protected float calculateSpeedPerPixel
                            (DisplayMetrics displayMetrics) {
                        return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                    }
                };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }
}

Here is a getting error on this line:

startSmoothScroll(linearSmoothScroller);

Error is - CustomLinearLayoutManager.smoothScrollToPosition java.lang.IllegalArgumentException: Invalid target position

Here is the code for the home fragment

 recyclerViewHeaderSlider.setLayoutManager(new CustomLinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        headerSliderAdapter.setOnClick(this);
        recyclerViewHeaderSlider.setAdapter(headerSliderAdapter);
        final int speedScroll = 6000;
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            int count = 0;
            boolean flag = true;

            @Override
            public void run() {
                if (count < headerSliderAdapter.getItemCount()) {
                    if (count == headerSliderAdapter.getItemCount() - 1) {
                        flag = false;
                    } else if (count == 0) {
                        flag = true;
                    }
                    if (flag) count++;
                    else count--;

                    recyclerViewHeaderSlider.smoothScrollToPosition(count);
                    handler.postDelayed(this, speedScroll);
                }
            }
        };

        handler.postDelayed(runnable, speedScroll);

Here is a getting error on this line:

recyclerViewHeaderSlider.smoothScrollToPosition(count);

Error is - java.lang.IllegalArgumentException: Invalid target position

like image 381
Saransh Agarwal Avatar asked Sep 14 '19 16:09

Saransh Agarwal


1 Answers

The error is due to a negative index (-1).

Look at this code:

if (count == headerSliderAdapter.getItemCount() - 1) {
    flag = false;
} else if (count == 0) {
    flag = true;
}

If your item count is 1, then the first if will be true when count == 0. 1 - 1 = 0 so flag = false.

Then, when you get to the second if:

if (flag) count++;
else count--;

flag is false so your code will execute count-- but count is already 0, thus you get count == -1.

Then you try to scroll to a negative position, which is not allowed.

like image 146
Lev M. Avatar answered Sep 28 '22 08:09

Lev M.