• 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方法,照着红色部分写。

  • 相关阅读:
    C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用(转)
    Web应用系统中关闭Excel进程
    jquery下一个空格带来的血案
    导出Excel时发生COM组件失败的解决方案
    水晶报表的交叉表中增加超级链接
    JavaScript和ExtJS的继承 Ext.extend Ext.applyIf (转)
    SQL SERVER 2000数据库置疑处理
    PHP中对淘宝URL中ID提取
    树莓派+蓝牙适配器连接蓝牙设备
    树莓派摄像头模块转成H264编码通过RTMP实现Html输出
  • 原文地址:https://www.cnblogs.com/wuyou/p/3524726.html
Copyright © 2020-2023  润新知