【问题标题】:Error in SherlockFragment when switching items in slide Menu在幻灯片菜单中切换项目时 SherlockFragment 出错
【发布时间】:2013-08-23 20:18:26
【问题描述】:

我在实现ActionBar Sherlock时遇到了很多问题,最后一个就是这个。我的操作栏中有一个带有 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 和一个从 Application 扩展而来的类,用于控制所有 WebService 通信。

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. 

}

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

【问题讨论】:

  • 我不确定,但我认为您使代码复杂化。尝试使用带有适配器的 ListView 而不是 LineraLayout lls。问题是您不止一次调用方法 public void call(final ArrayList users, int error) 。第一次工作正常,因为您添加的行与订单匹配。第二次,您正在添加新行,但由于 mviews.get(i) 仍添加第一行。我认为你能做的最好的就是改变这个,看看你创造的怪物:) for (i = mItems.size; i
  • @Juangcg 但我的 lls 是 Horizo​​ntalScrollView 内的 LinearLayout。经过一些尝试,我得出结论,这是显示一些图像的最佳方式(我现在不知道一开始有多少)y 水平滚动,比屏幕大。实际上它们不仅是图像,而且是由图像和文本视图组合而成的相对布局
  • 这个函数调用了多少次? public void call(final ArrayList users, int error)
  • 每次我去这个活动。也许我应该只调用一次,但我不知道如何保存所有这些视图的实例。
  • 在调用该函数之前尝试检查是否已经添加了视图。像这样: if(mViews==null || mViews.length == 0) 调用函数。

标签: android view actionbarsherlock fragment addchild


【解决方案1】:

使用这个

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

而不是

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

【讨论】:

  • 谢谢Juangcg,我试过了,但还是一样的错误。我将尝试提供有关我的视图父子结构的更多详细信息:
  • (对不起,我无意发送)谢谢Juangcg,我已经尝试过了,但仍然是同样的错误。我将尝试提供有关我的视图父子结构的更多详细信息:我将视图“v”作为上父级。在这个视图中,我有一个 LinearLayout“lls”和一个 ListView。在 linearLayout 中,我有“mviews”,这是一个创建水平滚动视图的视图数组(RelativeLayouts)。 Log 标记的行是我将 mviews[] 添加到“lls”时。希望它更清楚。欢迎任何帮助,因为这是我第一次使用 ActionBar,不知道如何解决。
  • 您收到的错误意味着当您尝试添加到列表时,您的水平滚动视图之一已经添加。我需要查看更多代码来帮助您。顺便说一句,当您说 3 个选项时,您的意思是 3 个选项卡?
  • 例如,mviews是否在fragment之间共享?你如何处理标签点击事件?
  • 好吧,对不起,让我们试着解释得更好。当我说选项时,我指的是 Navigation Drawer 中横向菜单的选项,例如新的 gmail 应用程序(您可以看到 link )。每个选项都会带您进行不同的活动,每个片段)。所以,如果我正在进行家庭活动并使用菜单导航到其他活动,那么当我尝试导航回到家时它会崩溃。 mviews 不与任何其他片段共享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2022-12-17
  • 2013-11-27
  • 2015-05-27
相关资源
最近更新 更多