gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:50:59 25 4
gpt4 key购买 nike

我正在尝试实现发现的圆形 float 操作菜单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

关于java - 指定的子级已经有一个父级 - 实现 FloatingActionMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29245095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com