致命异常:java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
我是 android 的新人。我在我的列表中进行了分页,并在单击时更改了 recyclerview 列表。
我的代码如下
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);
}
});
如果没有下一页或上一页,我将隐藏按钮。有时如果用户快速点击按钮,我会遇到致命错误。
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.access0(FragmentCat.java:35)
at com.karopass.fragment.FragmentCat.onClick(FragmentCat.java:95)
at android.view.View.performClick(View.java:7394)
at android.view.View.performClickInternal(View.java:7348)
at android.view.View.access00(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)
在线这个
ArrayList<ItemCategory> arraylistsub = new ArrayList<>(Constant.arrayListCategories.subList(From, To));
完整片段代码
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);
}
}
}
我不知道如何处理这种情况。让我知道这里是否有人可以帮助我解决问题。谢谢!
Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
说明你传错了索引,'to'必须大于'from'。你能展示你创建 'from' 和 'to' 索引的代码吗?
我是 android 的新人。我在我的列表中进行了分页,并在单击时更改了 recyclerview 列表。 我的代码如下
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);
}
});
如果没有下一页或上一页,我将隐藏按钮。有时如果用户快速点击按钮,我会遇到致命错误。
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.access0(FragmentCat.java:35)
at com.karopass.fragment.FragmentCat.onClick(FragmentCat.java:95)
at android.view.View.performClick(View.java:7394)
at android.view.View.performClickInternal(View.java:7348)
at android.view.View.access00(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)
在线这个
ArrayList<ItemCategory> arraylistsub = new ArrayList<>(Constant.arrayListCategories.subList(From, To));
完整片段代码
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);
}
}
}
我不知道如何处理这种情况。让我知道这里是否有人可以帮助我解决问题。谢谢!
Fatal Exception: java.lang.IllegalArgumentException: fromIndex(10) > toIndex(0)
说明你传错了索引,'to'必须大于'from'。你能展示你创建 'from' 和 'to' 索引的代码吗?