在幻灯片菜单中切换项目时SherlockFragment出错

时间:2013-08-21 12:44:58

标签: android view actionbarsherlock fragment addchild

我在实施ActionBar Sherlock时遇到很多问题,最后一个就是这个问题。我的ActionBar中有一个幻灯片菜单,有3个选项。我的问题是,当我选择一个先前已被选中的项目(它加载片段)时,应用程序崩溃。日志错误是

  

指定的孩子已经有父母。你必须调用removeView()   先关于孩子的父母。

它标记了我向Horizo​​ntalScroller添加视图的行。

lls.addView(mviews.get(i));

在我的OnCreateView中我有

final View v = inflater.inflate(R.layout.activity_landing, container,false);

container.removeAllViews();

我在这里发布了许多不同的方法,但我没有解决我的问题。有任何想法吗?

编辑: 这是我的MainActivity和Fragment的一些代码。

这是MainActivity。这是一个ScreenSplash之后的一个类,它从控制所有WebService通信的Application扩展而来。

public class MainActivity extends SherlockFragmentActivity  {


    //Declare Variables
    //...
     Fragment fragment1 = new Fragment1();
     Fragment fragment2 = new Fragment2();
     Fragment fragment3 = new Fragment3();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
        getSupportActionBar().setTitle("");          
        setContentView(R.layout.drawer_main);


        // Generate title
        title = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };

        // Generate icon
        icon = new int[] { R.drawable.item1, R.drawable.item2,
                R.drawable.item3, R.drawable.item4};

        // Locate DrawerLayout in drawer_main.xml
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Locate ListView in drawer_main.xml
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Pass results to MenuListAdapter Class
        mMenuAdapter = new MenuListAdapter(this, title, subtitle, icon);

        // Set the MenuListAdapter to the ListView
        mDrawerList.setAdapter(mMenuAdapter);

        // Capture button clicks on side menu
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // Enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }



    }


    //FOR ABS y ND
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
      //Define ActionBar buttons and actions 

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Sliding lateral Menu
        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    // The click listener for ListView in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment2);
            break;
        case 2:
            ft.replace(R.id.content_frame, fragment3);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

这是有错误的片段。现在其他两个都是空的。

public class Fragment1 extends SherlockFragment implements
    AdapterView.OnItemClickListener {

//DeclareVariables

public Fragment1() {
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //some declaration ad settings (witdhs, typefaces, caches,...)   

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    final View v = inflater.inflate(R.layout.main_activity, null,
            false);
    container.removeAllViews();
    lls = (LinearLayout) v.findViewById(R.id.lscroll_item);
    lls.removeAllViews();
            //Instantiate some elements of the view such as TextViews and ImageViews
            //layoutParams

    // Show Scroll 
    DataStore.sharedInstance().getInfo(
            new DataStore.infoReturn() {

        @Override
        public void call(final ArrayList<User> users, int error) {
            if(users != null){                  

                for (i = 0; i < users.size(); i++) {

                            mviews.add((RelativeLayout) getActivity().getLayoutInflater().inflate(R.layout.item_user, null));
                            mviews.get(i).setLayoutParams(layoutParams2);
                            imv = (ImageView) mviews.get(i).findViewById(R.id.user);
                            imv.getLayoutParams().height=friend_height;
                            imv_click = (ImageView) mviews.get(i).findViewById(R.id.click);
                            TextView text2 = (TextView) mviews.get(i).findViewById(R.id.fav);
                            text2.setTypeface(myTypeFace);
                                                            //set widths and layoutParams and sources



                            mviews.get(i).setId(i);
                                                            //NEXT LINE IS THE CRASH POINT, WHERE I TRY TO ADD ITEMS TO THE VIEW
                            lls.addView(mviews.get(i));

                            mviews.get(i).setOnClickListener(
                                    new OnClickListener() {

                                        @Override
                                        public void onClick(View v) {
                                            //Set actins when click
                                        }
                                    });

                        }
            }else{

                switch (error) {
                case -1: //ERROR OBTENER USERS                      
                    break;

                default:
                    break;
                }

            }
                }
            });



@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // actions when click

}

 @Override
    public void onResume() {
        super.onResume();

        refreshInfo();
            refreshSelectedUsers();
    }

    @Override
    public void onPause() {
        super.onPause();
        DataStore.sharedInstance().setSelectedUsers(mSelected);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //delete cache

    }



   //Some other methods for other UI items. 

}

我隐藏了一些代码,以便于阅读。

1 个答案:

答案 0 :(得分:0)

使用此

final View v = inflater.inflate(R.layout.activity_landing, null, false);

而不是

final View v = inflater.inflate(R.layout.activity_landing, container,false);