Java 添加三个片段会引发IllegalStateException

Java 添加三个片段会引发IllegalStateException,java,android,android-fragments,illegalstateexception,fragmentpageradapter,Java,Android,Android Fragments,Illegalstateexception,Fragmentpageradapter,我将生成的活动与滑动视图和标题栏一起使用。我的代码每个活动使用两个片段,但是当我添加三个片段时,它将强制关闭 错误是: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. fragment 这是我的活动类主菜单: //imports public class MainMenu exte

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

错误是:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. fragment
这是我的活动类主菜单:

//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类或删除视图有关,但我不知道如何修复它

非常感谢您的帮助


非常感谢

这里创建片段的方式是非标准的。我在无意中使用新产品时也遇到过类似的微妙问题。。。将新YourFragmentGoesher替换为Fragment.InstanceContext、YourFragmentGoesher.class.getName,bundle@OceanLife你的解决方案对我来说有点用…但是当尝试使用Pager.setCurrentItemindex从一个页面跳转到另一个随机页面时,会出现相同的异常…这里有什么建议吗?你在这里创建片段的方式是非标准的。我在无意中使用新产品时也遇到过类似的微妙问题。。。将新YourFragmentGoesher替换为Fragment.InstanceContext、YourFragmentGoesher.class.getName,bundle@OceanLife你的解决方案对我来说有点效果…但是当尝试使用Pager.setCurrentItemindex从一个页面跳转到另一个随机页面时,会出现相同的异常…这里有什么建议吗?