我试图在片段类中创建一个viewpager转换,该片段类本身派生自另一个片段类。每当我尝试创建viewpager时,它都会显示以下错误。
02-09 11:28:34.321: E/AndroidRuntime(8520): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gm.gakkmusic/com.gm.gakkmusic.MainActivity}: java.lang.IllegalStateException: Recursive entry to executePendingTransactions
02-09 11:28:34.321: E/AndroidRuntime(8520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
02-09 11:28:34.321: E/AndroidRuntime(8520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
在StackOverflow中搜索了一段时间后,我发现在将子片段嵌套到其主片段时我使用getChildFragmentManager()
但我对整个过程感到困惑。如果有人能解决这个问题并提供快速解决方案,那就太好了。
来源如下,
这是从MainActivity.class
创建Fragment1的地方 @Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, new Fragment1())
.commit();
break;
}
}
Fragment1.class是父Fragment,在其中创建了子viewpager转换,
public class Fragment1 extends Fragment implements OnItemClickListener {
private Fragment contentFragment;
SliderFragment sliderFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
if (savedInstanceState != null) {
if (fragmentManager.findFragmentByTag(SliderFragment.ARG_ITEM_ID) != null) {
sliderFragment = (SliderFragment) fragmentManager
.findFragmentByTag(SliderFragment.ARG_ITEM_ID);
contentFragment = sliderFragment;
}
} else {
sliderFragment = new SliderFragment();
switchContent(sliderFragment, SliderFragment.ARG_ITEM_ID);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View content = inflater.inflate(R.layout.fragment1_layout, container,
false);
return content;
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (contentFragment instanceof SliderFragment) {
outState.putString("content", SliderFragment.ARG_ITEM_ID);
}
super.onSaveInstanceState(outState);
}
public void switchContent(Fragment fragment, String tag) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
while (fragmentManager.popBackStackImmediate())
;
if (fragment != null) {
FragmentTransaction transaction = fragmentManager
.beginTransaction();
transaction.replace(R.id.content_frame, fragment, tag);
if (!(fragment instanceof SliderFragment)) {
transaction.addToBackStack(tag);
}
transaction.commit();
contentFragment = fragment;
}
}
public void onBackPressed() {
FragmentManager fm = getActivity().getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
super.getActivity().onBackPressed();
} else if (contentFragment instanceof SliderFragment
|| fm.getBackStackEntryCount() == 0) {
getActivity().finish();
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(LOG_TAG, "onItemClick: " + position);
Log.d(LOG_TAG, "checked items: " + listView.getCheckedItemCount());
Log.d(LOG_TAG,
"checked positions: " + listView.getCheckedItemPositions());
}
}
更新:我已经通过将所有getSupportFragmentManager()
替换为getChildFragmentManager()
来解决问题,但我仍在寻找解决此问题的有效方法。