问题
I have viewPager which contains fragments each of these fragment has viewpager to display Images and banners
now to each fragment Image adapter I am passing getChildFragmentManager() as below
CarouselAdapter carouselAdapter = new CarouselAdapter(getChildFragmentManager());
But above code is causing crash while changing viewpager tabs showing following message
java.lang.IllegalArgumentException: No view found for id 0x7f1000ef
But if I pass getFragmentManager() it is not crashing but adapter goes blank for some time any Idea how to handle nested fragments.
I am using FragmentPagerAdapter for both viewpagers but still its not working
Following is crashlog
java.lang.IllegalArgumentException: No view found for id 0x7f1000ef (com.packagename:id/viewPagerBanners) for fragment MainBannerFragmentNew{308cb84 #0 id=0x7f1000ef android:switcher:2131755247:0}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1523)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1585)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2832)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2199)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1323)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:273)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Following is carousel Adapter code
public class CarouselAdapter extends FragmentPagerAdapter {
private List<CarouselItem> carouselItemsList;
public CarouselAdapter(FragmentManager fm, List<CarouselItem> carouselItemsList) {
super(fm);
this.carouselItemsList = carouselItemsList;
}
@Override
public Fragment getItem(int position) {
return MainBannerFragmentNew.newInstance(R.drawable.gray_logo, carouselItemsList.get(position));
}
@Override
public int getCount() {
return carouselItemsList.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
Following is sliding tab adapter code
public class HomeTabsPagerAdapter extends FragmentPagerAdapter {
ArrayList<HomeTabItem> homeTabItems;
public HomeTabsPagerAdapter(FragmentManager manager, ArrayList<HomeTabItem> homeTabItems) {
super(manager);
this.homeTabItems = homeTabItems;
}
@Override
public Fragment getItem(int position) {
HomeTabItem homeTabItem = homeTabItems.get(position);
return OfferZoneFragment.newInstance(homeTabItem.getId());
}
@Override
public int getCount() {
return homeTabItems.size();
}
@Override
public CharSequence getPageTitle(int position) {
return homeTabItems.get(position).getTitle();
}
}
Every sliding tab fragment has recyclerview this recylerview contains carousel viewpager following is the code in bindview
((BannerHolder) holder).viewPager.setAdapter(new CarouselAdapter(fragmentManager, homePageItems.get(position).carouselItems));
((BannerHolder) holder).circlePageIndicator.setViewPager(((BannerHolder) holder).viewPager);
final float density = context.getResources().getDisplayMetrics().density;
((BannerHolder) holder).circlePageIndicator.setBackgroundColor(0x00000000);
((BannerHolder) holder).circlePageIndicator.setRadius(5 * density);
((BannerHolder) holder).circlePageIndicator.setFillColor(ContextCompat.getColor(context, R.color.appthemecolor));
((BannerHolder) holder).circlePageIndicator.setPageColor(0xFFFFFFFF);
((BannerHolder) holder).circlePageIndicator.setStrokeColor(0x00000000);
((BannerHolder) holder).circlePageIndicator.setStrokeWidth(2 * density);
if (((BannerHolder) holder).viewPager.getAdapter().getCount() <= 1) {
((BannerHolder) holder).circlePageIndicator.setVisibility(View.GONE);
} else {
((BannerHolder) holder).circlePageIndicator.setVisibility(View.VISIBLE);
}
回答1:
You are using getChildFragmentManager
for nested fragments which is correct.
What you are doing:
In your case when the fragments wants to be attached to the (child's) viewpager but here the viewpager is not attached to its parent (still detached).
What you should do:
Create a custom ViewPager
override its onAttachedToWindow
and do the lazy set adapter.
For more details have a look at IllegalArgumentException: No view found for id for fragment — ViewPager in ViewPager.
Update:
If you want to use getFragmentManager
or getSupportFragmentManager
than change the parent class of your PagerAdapter
from android.support.v4.app.FragmentPagerAdapter
to android.support.v4.app.FragmentStatePagerAdapter
.
回答2:
Replaced FragmentPagerAdapter by FragmentStatePagerAdapter for both child and parent fragment solved issue for me.
来源:https://stackoverflow.com/questions/41806185/nested-fragments-getchildfragmentmanager-causing-crash