当前位置:  开发笔记 > Android > 正文

嵌套片段getchildfragmentmanager导致崩溃

如何解决《嵌套片段getchildfragmentmanager导致崩溃》经验,为你挑选了1个好方法。

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

现在每个片段Image适配器我传递的是getChildFragmentManager(),如下所示

CarouselAdapter carouselAdapter = new CarouselAdapter(getChildFragmentManager());

但是上面的代码在更改显示以下消息的viewpager选项卡时导致崩溃

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

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

我正在使用FragmentPagerAdapter用于两个viewpage,但仍然无法正常工作

以下是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 carouselItemsList;

public CarouselAdapter(FragmentManager fm, List 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 homeTabItems;

public HomeTabsPagerAdapter(FragmentManager manager, ArrayList 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);
                }

Haris Quresh.. 7

您正在使用getChildFragmentManager正确的嵌套片段.

你在做什么:

在你的情况下,片段想要附加到(孩子的)viewpager,但这里viewpager没有附加到它的父(仍然分离).

你应该做什么:

创建一个自定义ViewPager覆盖它onAttachedToWindow并执行惰性集适配器.

有关更多详细信息,请查看IllegalArgumentException:找不到用于片段的id的视图 - ViewPager中的ViewPager.

更新:

如果你想使用getFragmentManagergetSupportFragmentManager比父类的的改变PagerAdapter,从android.support.v4.app.FragmentPagerAdapterandroid.support.v4.app.FragmentStatePagerAdapter.



1> Haris Quresh..:

您正在使用getChildFragmentManager正确的嵌套片段.

你在做什么:

在你的情况下,片段想要附加到(孩子的)viewpager,但这里viewpager没有附加到它的父(仍然分离).

你应该做什么:

创建一个自定义ViewPager覆盖它onAttachedToWindow并执行惰性集适配器.

有关更多详细信息,请查看IllegalArgumentException:找不到用于片段的id的视图 - ViewPager中的ViewPager.

更新:

如果你想使用getFragmentManagergetSupportFragmentManager比父类的的改变PagerAdapter,从android.support.v4.app.FragmentPagerAdapterandroid.support.v4.app.FragmentStatePagerAdapter.

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有