指定的子项已经有一个父项 - 实现FloatingActionMenu

时间:2015-03-24 23:55:08

标签: java android imageview material-design floating-action-button

我正在尝试实现找到here的循环浮动操作菜单,但是当我运行该应用程序时,我收到此错误:

  

java.lang.IllegalStateException:指定的子节点已经有了   家长。您必须首先在孩子的父母身上调用removeView()。

我对Android材料设计和实现自定义动画等非常陌生。我已经看到了类似的问题,但每个问题似乎都是针对特定的背景。

这是我的OnCreate()方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        ImageView icon = new ImageView(this); // Create an icon
        icon.setImageDrawable(getResources().getDrawable(R.drawable.abc_ic_search_api_mtrl_alpha));

        FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(icon)
                .build();

        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);
        ImageView itemIcon1 = new ImageView(this);
        itemIcon1.setImageDrawable(getResources().getDrawable(R.drawable.abc_ic_search_api_mtrl_alpha));
        SubActionButton button1 = itemBuilder.setContentView(itemIcon1).build();
        ImageView itemIcon2 = new ImageView(this);
        itemIcon2.setImageDrawable(getResources().getDrawable(R.drawable.button_action_dark_touch));
        SubActionButton button2 = itemBuilder.setContentView(itemIcon2).build();
        ImageView itemIcon3 = new ImageView(this);
        itemIcon3.setImageDrawable(getResources().getDrawable(R.drawable.button_action_selector));
        SubActionButton button3 = itemBuilder.setContentView(itemIcon2).build();

        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(button1)
                .addSubActionView(button2)
                .addSubActionView(button3)
                .attachTo(actionButton)
                .build();
        setContentView(R.layout.activity_main);
    }

1 个答案:

答案 0 :(得分:2)

ImageView itemIcon2 = new ImageView(this);
        itemIcon2.setImageDrawable(getResources().getDrawable(R.drawable.button_action_dark_touch));
        SubActionButton button2 = itemBuilder.setContentView(itemIcon2).build();
        ImageView itemIcon3 = new ImageView(this);
        itemIcon3.setImageDrawable(getResources().getDrawable(R.drawable.button_action_selector));
        SubActionButton button3 = itemBuilder.setContentView(itemIcon2).build();

itemBuilder.setContentView(itemIcon2)被调用两次:P 最后一行应为itemIcon3