英文:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 on backpressed fragment
问题
在我的MainActivity中,我有以下类似的代码:
if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
String title = getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getBackStackEntryCount() - 1).getTag();
if (title != null) {
method.ShowFullScreen(false);
toolbar.setTitle(title);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().clearFlags(1024);
}
super.onBackPressed();
}
在我的Fragment中,我从分类Fragment切换到搜索Fragment,代码如下:
SearchFragment searchFragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
searchFragment.setArguments(bundle);
Objects.requireNonNull(getActivity()).getSupportFragmentManager()
.beginTransaction().replace(R.id.frameLayout_main, searchFragment, tag).addToBackStack(category_name).commitAllowingStateLoss();
现在,当从搜索Fragment返回时,我遇到以下错误:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.myapp.Activity.MainActivity.onBackPressed(MainActivity.java:225)
我正在学习Android,不太清楚为什么会崩溃。如果有人能帮我解决这个问题,我会非常感谢!
英文:
I have code in my MainActivity like below
if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
String title = getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getBackStackEntryCount() - 1).getTag();
if (title != null) {
method.ShowFullScreen(false);
toolbar.setTitle(title);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().clearFlags(1024);
}
super.onBackPressed();
}
In my Fragment, I am going from category fragment to search fragment like below
SearchFragment searchFragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
searchFragment.setArguments(bundle);
Objects.requireNonNull(getActivity()).getSupportFragmentManager()
.beginTransaction().replace(R.id.frameLayout_main, searchFragment, tag).addToBackStack(category_name).commitAllowingStateLoss();
Now when pressing back from the search fragment, I am getting an error like below
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.myapp.Activity.MainActivity.onBackPressed(MainActivity.java:225)
I am learning android and do not get an idea of why it's getting crashed. Let me know if someone can help me with solving the issue.
Thanks!
答案1
得分: 0
你遇到了IndexOutOfBoundsException。你的数组大小为1,而唯一可用的索引是0,而不是1。请根据堆栈跟踪转到ArrayList.java的第438行或MainActivity.java的第225行。检查索引值,例如getSupportFragmentManager().getBackStackEntryCount()的返回值。
英文:
You have an IndexOutOfBoundsException. Your array has a size of one and the only index available is at 0, not at 1. Follow your stacktrace to line 438 in ArrayList.java or line 225 in MainActivity.java. Inspect the index values, like the return value of getSupportFragmentManager().getBackStackEntryCount().
答案2
得分: 0
尝试不替换当前片段,在当前片段之上添加新片段并隐藏当前片段。
SearchFragment searchFragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
searchFragment.setArguments(bundle);
Objects.requireNonNull(getActivity())
.getSupportFragmentManager()
.beginTransaction()
.add(R.id.frameLayout_main, searchFragment, tag)
.addToBackStack(category_name)
.hide(currentFragment)
.commitAllowingStateLoss();
英文:
Try to not replace current fragment, add new on top of current fragment and hide current fragment.
SearchFragment searchFragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
searchFragment.setArguments(bundle);
Objects.requireNonNull(getActivity())
.getSupportFragmentManager()
.beginTransaction()
.add(R.id.frameLayout_main, searchFragment, tag)
.addToBackStack(category_name)
.hide(currentFragment)
.commitAllowingStateLoss();
</details>
# 答案3
**得分**: 0
你正在对片段使用**replace**方法,并且还将片段添加到了后退堆栈中。这实际上不是一个好的做法。在开始片段事务时,您需要使用**add**代替replace。
将这个
```java
.replace(R.id.frameLayout_main, searchFragment, tag)
替换为这个
.add(R.id.frameLayout_main, searchFragment, tag)
另外,为了解决这个崩溃问题,将下面的代码
if (getSupportFragmentManager().getBackStackEntryCount() != 0)
替换为这个
if (getSupportFragmentManager().getBackStackEntryCount() != 0 &&
getSupportFragmentManager().getBackStackEntryCount() < getSupportFragmentManager().getFragments().size())
英文:
You are using replace method for fragments and also you are adding fragments to backstack. WWhich is not a good approach really. You need to use add in place of replace while beginning a transaction for fragment.
Replacethis
.replace(R.id.frameLayout_main, searchFragment, tag)
with this
.add(R.id.frameLayout_main, searchFragment, tag)
Also to tackle this crash , replace below line
if (getSupportFragmentManager().getBackStackEntryCount() != 0)
with this one
if (getSupportFragmentManager().getBackStackEntryCount() != 0 &&
getSupportFragmentManager().getBackStackEntryCount() < getSupportFragmentManager().getFragments().size())
答案4
得分: 0
我认为你可以在你的片段中尝试这样做:
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
SearchFragment searchFragment = new SearchFragment();
searchFragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout_main, searchFragment)
.addToBackStack(category_name)
.commit();
而在你的活动中:
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
}
英文:
I think you can try this, in your fragment
Bundle bundle = new Bundle();
bundle.putString("search_menu", tag);
bundle.putString("typeLayout", "Landscape");
SearchFragment searchFragment = new SearchFragment();
searchFragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout_main, searchFragment)
.addToBackStack(category_name)
.commit();
And in your Activity
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论