0

我想實現的圓形浮動操作菜單中here但只要我跑的應用程序,我得到這個錯誤:指定的孩子已經有一個父 - 實施FloatingActionMenu

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

我很新到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
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

+0

嗯,這是愚蠢的..不過已經習慣了這個東西。 – 2015-03-25 23:36:06

相關問題