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

The specified child already has a parent - implementing FloatingActionMenu

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

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

我对 Android material 设计和实现自定义动画等非常陌生。我看到过与此类似的问题,但每个问题似乎都是针对特定上下文的。

这是我的 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);
    }
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