【问题标题】:Dynamic Tab contents with fragment带有片段的动态选项卡内容
【发布时间】:2014-04-24 17:01:06
【问题描述】:

我有以下 android 代码。当我向前浏览选项卡时它工作正常,但是一旦我尝试向后滑动应用程序就会崩溃。 问题出在“rootView = (ScrollView) gTabView.get(t);”线上当它尝试从全局变量第二次分配视图时。 我怎样才能避免这种情况? 谢谢

public class DocPageActivity extends FragmentActivity {
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    ViewPager mViewPager;  


    public static ActionBar actionBar = null;
    public static int gtab=0;
    public static ArrayList gTabView  = new ArrayList();


    /** Called when the activity is first created. */
    @Override
     public void onCreate(Bundle savedInstanceState) {
        //InstState =  savedInstanceState;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager_obj);
        gTabView.clear();
        gtab = 0;
       BuildTabsContents();//creates view to dislay in each tab in gTabView,store countNo of tabs in gtab
        mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());

        // Set up action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home button should show an "Up" caret, indicating that touching the
        // button will take the user one step up in the application's hierarchy.
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Set up the ViewPager, attaching the adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);


        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}


   /**
     * A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment
     * representing an object in the collection.
     */

    public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {


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

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DemoObjectFragment();
            Bundle args = new Bundle();
            args.putInt(DemoObjectFragment.ARG_OBJECT, i); 

            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return gtab; 
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return "TAB " + (position + 1);
        }
    }


    /**
     * A dummy fragment representing a section of the app, but that simply displays dummy text.
     */
    public static class DemoObjectFragment extends Fragment {
        public static final String ARG_OBJECT = "object";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView;
            Bundle args = getArguments();
            int t = args.getInt(ARG_OBJECT);            

             rootView = (ScrollView) gTabView.get(t);

            return rootView;         

        }
    }
}

【问题讨论】:

    标签: android tabs android-actionbar fragment


    【解决方案1】:

    我通过在类级别声明 rootview 并覆盖下面的方法解决了我的问题。

       @Override
       public void onDestroyView() {
       super.onDestroyView();
        if (rootView != null) {
    
            ViewGroup parentViewGroup = (ViewGroup) rootView.getParent();
            if (parentViewGroup != null) {
                parentViewGroup.removeAllViews();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      相关资源
      最近更新 更多