2

我将生成的活动与滑动视图和标题条一起使用。我的代码在每个活动中使用两个片段,但是当我添加三个片段时,它会强制关闭。

错误是:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. fragment

这是我的活动类 MainMenu:

//imports

public class MainMenu extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_menu, menu);
        return true;
    }




    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new Fragment();
            Bundle args = new Bundle();
            if(i == 0){
                fragment = new InsertCity();
            }else if(i == 1){
                fragment = new AddFlight();
            }else if(i == 2){
                fragment = new AddFlight();
            }
            fragment.setArguments(args);
            return fragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return getString(R.string.title_section1).toUpperCase();
                case 1: return getString(R.string.title_section2).toUpperCase();
                case 2: return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }


}

每个片段几乎是相同的,所以我只包括其中一个。

这是我的 InsertCity 类:

public class InsertCity extends Fragment {

    private LinearLayout ll;

    public InsertCity() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ll = new LinearLayout(getActivity());

        ll.setOrientation(LinearLayout.VERTICAL);

        TextView usernameInstruction = new TextView(getActivity());
        ll.addView(usernameInstruction);

        Button btn = new Button(getActivity());
        btn.setText("Create Backup");
        btn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {


            }
        });

        ll.addView(btn);
        // setContentView(ll);

    }

//  @Override
//  public void onDestroyView() {
//      super.onDestroyView();
//      ViewGroup parentViewGroup = (ViewGroup) ll.getParent();
//      if (null != parentViewGroup) {
//          parentViewGroup.removeView(ll);
//      }
//  }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if(ll != null) { return ll; }
        return ll;
    }

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

}

我很确定这与我的 SectionsPagerAdapter 类或删除视图有关,但我不知道如何解决它。

帮助将不胜感激。

非常感谢!

4

0 回答 0