zoukankan      html  css  js  c++  java
  • android fragment嵌套fragment出现的问题:no activity

    package com.example.fragmentNavigation2.fragment;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.ContextThemeWrapper;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.example.fragmentNavigation2.R;
    import com.viewpagerindicator.TabPageIndicator;
    
    import java.lang.reflect.Field;
    
    public class AddressFragment extends Fragment {
    
        private static final String[] CONTENT = new String[]{"协会简介", "矿业新闻", "活动与交流", "会员中心"};
        private FragmentManager fragmentManager;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            fragmentManager = getChildFragmentManager();
            final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), com.viewpagerindicator.R.style.Theme_PageIndicatorDefaults);
            // clone the inflater using the ContextThemeWrapper
            LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
            View view = localInflater.inflate(R.layout.simple_tabs, container, false);
    
            FragmentPagerAdapter adapter = new FragmentPagerAdapter(fragmentManager) {
    
                @Override
                public Fragment getItem(int position) {
                    return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
                }
    
                @Override
                public CharSequence getPageTitle(int position) {
                    return CONTENT[position % CONTENT.length].toUpperCase();
                }
    
                @Override
                public int getCount() {
                    return CONTENT.length;
                }
            };
    
            ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
            pager.setAdapter(adapter);
    
            TabPageIndicator indicator = (TabPageIndicator) view.findViewById(R.id.indicator);
            indicator.setViewPager(pager);
            indicator.setFillViewport(true);
            return view;
        }
    
        /**
         * 这段可以解决fragment嵌套fragment会崩溃的问题
         */
        @Override
        public void onDetach() {
            super.onDetach();
            try {
                //参数是固定写法
                Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    解决方案为红色部分,获取fragmentManager时要用getChildFragmentManager()这个方法。

    还要重写onDetach方法,照着红色部分写。

  • 相关阅读:
    《剑指Offer》题目:跳台阶
    《剑指Offer》题目:变态跳台阶
    《剑指Offer》题目:二叉树的镜像
    《剑指Offer》题目:树的子结构
    《剑指Offer》题目:合并两个排序的链表
    《剑指Offer》题目:链表中倒数第k个结点
    《剑指Offer》题目:调整数组顺序使奇数位于偶数前面
    Nginx配置http强制跳转到https
    并查集详解(转)
    AKOJ -- 1529 -- 寻找最大数
  • 原文地址:https://www.cnblogs.com/wuyou/p/3524726.html
Copyright © 2011-2022 走看看