嵌套片段getchildfragmentmanager导致崩溃

时间:2017-01-23 12:17:23

标签: android android-fragments android-viewpager android-support-library android-nested-fragment

我有viewPager,其中包含这些片段中的每个片段都有viewpager来显示图像和横幅

现在每个片段图像适配器我传递getChildFragmentManager()如下所示

CarouselAdapter carouselAdapter = new CarouselAdapter(getChildFragmentManager());

但是上面的代码在更改显示以下消息的viewpager标签时会导致崩溃

java.lang.IllegalArgumentException: No view found for id 0x7f1000ef 

但是,如果我传递getFragmentManager()它没有崩溃,但适配器空白一段时间任何想法如何处理嵌套片段。

我正在为两个浏览器使用FragmentPagerAdapter,但仍无法正常工作

以下是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)

以下是轮播适配器代码

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);
}
}

以下是滑动标签适配器代码

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();
}
}

每个滑动标签片段都有recyclerview,这个rec​​ylerview包含carousel viewpager以下是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);
                }

2 个答案:

答案 0 :(得分:7)

您正在使用With phones as ( SELECT phone_number from customers ) ,emails as ( SELECT emails from customers ) select * from phones--/emails 来表示正确的嵌套片段。

你在做什么:

在您希望将片段附加到(child's)viewpager的情况下,但此处viewpager未附加到其父级(仍然是分离的)。

你应该做什么:

创建自定义getChildFragmentManager覆盖其ViewPager并执行延迟设置适配器。

有关详细信息,请查看IllegalArgumentException: No view found for id for fragment — ViewPager in ViewPager

<强>更新

如果您要使用onAttachedToWindowgetFragmentManager而不是将getSupportFragmentManager的父类从PagerAdapter更改为android.support.v4.app.FragmentPagerAdapter

答案 1 :(得分:0)

FragmentStatePagerAdapter替换了FragmentPagerAdapter,为我和子片段解决了问题。