Home > Software design >  Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)

Time:10-10

I am new in android. I have made pagination in my list and on click, its change recyclerview list. My code is like below

private void setAdapter(int From, int To) {

        textPageCount.setText(CURRENT_PAGE  "/"  TOTAL_PAGE);
        butPrev.setVisibility((CURRENT_PAGE ==1) ? View.INVISIBLE : View.VISIBLE);
        butNext.setVisibility((CURRENT_PAGE == TOTAL_PAGE) ? View.INVISIBLE : View.VISIBLE);

        progressBar.setVisibility(View.GONE);
        ArrayList<ItemCategory> arraylistsub = new ArrayList<>(Constant.arrayListCategories.subList(From, To));
        adapterCat = new AdapterCat(arraylistsub);
        adapterCat.notifyDataSetChanged();
        recyclerView.setAdapter(adapterCat);
        setEmpty();
    }

butNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CURRENT_PAGE = CURRENT_PAGE 1;
                FROM = (CURRENT_PAGE-1)* Constant.ITEM_PER_PAGE;
                TO = FROM   Constant.ITEM_PER_PAGE;
                if(TO>TOTAL){
                    TO = TOTAL;
                }
                setAdapter(FROM,TO);
            }
        });
        butPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CURRENT_PAGE = CURRENT_PAGE-1;

                FROM = (CURRENT_PAGE-1) * Constant.ITEM_PER_PAGE;
                TO = FROM   Constant.ITEM_PER_PAGE;
                if(TO>TOTAL){
                    TO = TOTAL;
                }
                setAdapter(FROM,TO);
            }
        });

I am hiding button if there no next or previous pages. Still sometime if user speedy click the buttons, I am getting below fatal error.

Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
       at java.util.ArrayList.subListRangeCheck(ArrayList.java:1018)
       at java.util.ArrayList.subList(ArrayList.java:1008)
       at com.karopass.fragment.FragmentCat.setAdapter(FragmentCat.java:192)
       at com.karopass.fragment.FragmentCat.access$200(FragmentCat.java:35)
       at com.karopass.fragment.FragmentCat$2.onClick(FragmentCat.java:95)
       at android.view.View.performClick(View.java:7394)
       at android.view.View.performClickInternal(View.java:7348)
       at android.view.View.access$3900(View.java:827)
       at android.view.View$PerformClick.run(View.java:28293)
       at android.os.Handler.handleCallback(Handler.java:899)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:238)
       at android.app.ActivityThread.main(ActivityThread.java:7864)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)

On line this

ArrayList<ItemCategory> arraylistsub = new ArrayList<>(Constant.arrayListCategories.subList(From, To));

Full Fragment code

public class FragmentCat extends Fragment {

    private Methods methods;
    private RecyclerView recyclerView;
    private AdapterCat adapterCat;
    private ProgressBar progressBar;

    private TextView textView_empty,textPageCount;
    private LinearLayout ll_empty;
    private String errr_msg;
    private AppCompatButton button_try;
    private int CURRENT_PAGE =1;
    private int TOTAL_PAGE= 0;
    private RelativeLayout butNext, butPrev;
    private int FROM  = 0, TO =0, TOTAL =0;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_category, container, false);


        methods = new Methods(getActivity());


        ll_empty = rootView.findViewById(R.id.ll_empty);
        textView_empty = rootView.findViewById(R.id.tv_empty_msg);
        button_try = rootView.findViewById(R.id.button_empty_try);
        butNext = rootView.findViewById(R.id.butNext);
        butPrev = rootView.findViewById(R.id.butPrev);
        textPageCount = rootView.findViewById(R.id.textPageCount);

        LinearLayout ll_adView = rootView.findViewById(R.id.ll_adView);
        methods.showBannerAd(ll_adView);



        TOTAL = Constant.arrayListCategories.size();
        TOTAL_PAGE = TOTAL/Constant.ITEM_PER_PAGE ((TOTAL%Constant.ITEM_PER_PAGE)>0?1:0);
        FROM = (CURRENT_PAGE-1) * Constant.ITEM_PER_PAGE;
        TO = FROM   Constant.ITEM_PER_PAGE;
        if(TO>TOTAL){
            TO = TOTAL;
        }

        button_try.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAdapter(FROM,TO);
            }
        });

        butNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CURRENT_PAGE = CURRENT_PAGE 1;
                FROM = (CURRENT_PAGE-1)* Constant.ITEM_PER_PAGE;
                TO = FROM   Constant.ITEM_PER_PAGE;
                if(TO>TOTAL){
                    TO = TOTAL;
                }
                setAdapter(FROM,TO);
            }
        });
        butPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CURRENT_PAGE = CURRENT_PAGE-1;

                FROM = (CURRENT_PAGE-1) * Constant.ITEM_PER_PAGE;
                TO = FROM   Constant.ITEM_PER_PAGE;
                if(TO>TOTAL){
                    TO = TOTAL;
                }
                setAdapter(FROM,TO);
            }
        });


        progressBar = rootView.findViewById(R.id.my_progressBar);

        recyclerView = rootView.findViewById(R.id.rv_author);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(gridLayoutManager);

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                int FinalPosition = position;
                if(CURRENT_PAGE>1){
                    FinalPosition = ((CURRENT_PAGE-1)*Constant.ITEM_PER_PAGE) position;
                }

                String id = Constant.arrayListCategories.get(FinalPosition).getId();
                Intent intent = new Intent(getActivity(), QuotesByCatActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name", Constant.arrayListCategories.get(FinalPosition).getName());
                intent.putExtra("TOTAL", Constant.arrayListCategories.get(FinalPosition).getTotal());
                intent.putExtra("Push", false);
                startActivity(intent);
            }
        }));



        setAdapter(FROM,TO);
        setHasOptionsMenu(true);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main, menu);

        final MenuItem searchItem = menu.findItem(R.id.menu_search);
        searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
        final SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener((new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @SuppressLint("NotifyDataSetChanged")
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!searchView.isIconified()) {
                    adapterCat.getFilter().filter(newText);
                    adapterCat.notifyDataSetChanged();
                }
                return true;
            }
        }));
        super.onCreateOptionsMenu(menu, inflater);
    }

    private int getPos(String id) {
        int count = 0;
        int rid = Integer.parseInt(id);
        for (int i = 0; i < Constant.arrayListCategories.size(); i  ) {
            if (rid == Integer.parseInt(Constant.arrayListCategories.get(i).getId())) {
                count = i;
                break;
            }
        }
        return count;
    }


    private void setAdapter(int From, int To) {

        textPageCount.setText(CURRENT_PAGE  "/"  TOTAL_PAGE);
        butPrev.setVisibility((CURRENT_PAGE ==1) ? View.INVISIBLE : View.VISIBLE);
        butNext.setVisibility((CURRENT_PAGE == TOTAL_PAGE) ? View.INVISIBLE : View.VISIBLE);

        progressBar.setVisibility(View.GONE);
        ArrayList<ItemCategory> arraylistsub = new ArrayList<>(Constant.arrayListCategories.subList(From, To));
        adapterCat = new AdapterCat(arraylistsub);
        adapterCat.notifyDataSetChanged();
        recyclerView.setAdapter(adapterCat);
        setEmpty();
    }

    private void setEmpty() {
        if (Constant.arrayListCategories.size() > 0) {
            recyclerView.setVisibility(View.VISIBLE);
            ll_empty.setVisibility(View.GONE);
        } else {
            textView_empty.setText(errr_msg);
            recyclerView.setVisibility(View.GONE);
            ll_empty.setVisibility(View.VISIBLE);
        }
    }
}

I am not getting idea how I can handle this situation. Let me know if anyone here can help me for solve the issue. Thanks!

CodePudding user response:

Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)

It means you passed the wrong index, 'to' must be greater than 'from'. Could you show the code you create 'from' and 'to' index?

  • Related