屏幕旋转后FragmentPagerAdapter.getItem(position)->“片段未附加到上下文”

时间:2018-10-31 13:37:37

标签: android android-viewpager fragmentpageradapter

我有一个Viewpager,其中有 4个片段。我滑动到我的第4 个片段(ResultFragment,其中包含一个称为ResultItemFragment的嵌套片段),然后旋转设备屏幕。然后,我得到一个异常ResultItemFragment{1842070} not attached to a context

我完全不知道为什么会这样。

Activity onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mobile_plan);

    final ViewPager pager = findViewById(R.id.viewPager_mobileplan);
    pager.setOffscreenPageLimit(5);

    adapter = new MobilePlanAdapter(getSupportFragmentManager());
    pager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }

        @Override
        public void onPageSelected(int i) {
            if (i == 3) {
                    ResultFragment resultFragment = (ResultFragment) adapter.getItem(i);


                    // ----------------------------
                    // java.lang.IllegalStateException: Fragment ResultItemFragment{1842070} not attached to a context.
                    // ----------------------------
                    resultFragment.showResult();
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }
    });
}

寻呼机适配器:

public class MobilePlanAdapter extends FragmentPagerAdapter {

    private Fragment[] fragments = new Fragment[4];

    public MobilePlanAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment frag = fragments[position];
        if (frag == null)
        {
            switch (position) {
                case 0:
                    frag = new CallTimeFragment();
                    break;

                case 1:
                    frag = new DataConsumptionFragment();
                    break;

                case 2:
                    frag = new ExtraOptionsFragment();
                    break;

                case 3:
                    frag = new ResultFragment();
                    break;

                default:
                    throw new IllegalStateException();
            }
            fragments[position] = frag;
        }
        return frag;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object ret = super.instantiateItem(container, position);
        fragments[position] = (Fragment) ret;
        return ret;
    }


    @Override
    public int getCount() {
        return fragments.length;
    }
}

ResultFragment

public class ResultFragment extends Fragment {

    ResultItemFragment fragmentPlanSim;
    ResultItemFragment fragmentPlanSimMobile;

    public ResultFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_result, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (fragmentPlanSim == null || fragmentPlanSimMobile == null) {
            fragmentPlanSim = inflateFragment(R.id.result_fragment_sim, TYPE_SIM);
            fragmentPlanSimMobile = inflateFragment(R.id.result_fragment_simmobile, TYPE_SIM_MOBILE);
        }
    }

    public void showResult() {
        fragmentPlanSim.setMobilePlan();
        fragmentPlanSimMobile.setMobilePlan();
    }

    private ResultItemFragment inflateFragment(int id, String type) {
        ResultItemFragment fragment = new ResultItemFragment();
        Bundle arguments = new Bundle();
        arguments.putString(ResultItemFragment.KEY_TYPE, type);
        fragment.setArguments(arguments);

        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(id, fragment);
        ft.commit();
    }
}

ResultItemFragment(嵌套在ResultFragment中):

public class ResultItemFragment extends ConfiguratorFragment {

    public ResultItemFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_result_item, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    public void setMobilePlan(Configurator.MobilePlan plan, Configurator.ExtraOption extraOption) {
        // do stuff
    }

}

0 个答案:

没有答案