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

  • 相关阅读:
    一文梳理Ubuntu下Eigen矩阵运算库总结教程
    Ubuntu下安装与使用Eigen矩阵运算库教程
    Ubuntu下cmake教程实践从入门到会用
    collection of vim vim tutorial for beginner
    利用ipython实现多线程
    如何快速地从mongo中提取数据到numpy以及pandas中去
    Git Push 避免用户名和密码方法
    如何使用scikit—learn处理文本数据
    format格式
    fk输入地壳模型容易出错的地方
  • 原文地址:https://www.cnblogs.com/wuyou/p/3524726.html
Copyright © 2011-2022 走看看