Specified child has already a parent

Asked by on 2012-10-07T15:53:40-04:00
this question had been asked here numerous times, however I couldn't find help in any of them. I'm writing an android application and seem to have a problem with creating tabs on the ActionBar. I've been looking for the problem for a couple of hours and can't seem to find the solution, however I think I did find the source. I've even emptied the implementations just to get some results and build upon them but alas I didn't find anything.

this is the creating of the tab fragment and insertion to the actionbar

 // put ActionBar to nav. mode
 ActionBar actionBar = getActionBar();

 // init "YES" tab
 Fragment yesTabFragment = Fragment.instantiate(this, YesTabFragment.class.getName());
 FlowTabListener yesTabListener = new FlowTabListener(yesTabFragment);
 ActionBar.Tab yesTab = actionBar.newTab().setText("YES").setTabListener(yesTabListener);
this is the onCreateView method of the class

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
 _fragmentView = inflater.inflate(R.layout.yes_fragment, container); 
and this is the fragment XML layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" >

as you can see, the implementations are empty, and i do not instantiate this fragment before this time (and not after) however I get this error:

E/AndroidRuntime(2995): FATAL EXCEPTION: main
E/AndroidRuntime(2995): java.lang.RuntimeException: Unable to start activity ComponentInfo{activities.MainActivity}: 
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I think it has something to do with the addition of the tab to the action bar since when I comment out the actionBar.addTab() the application does run (without the tabs obviously)

I would really like for a hint on how to sort this out.

Thanks a head guys

Best Answer

Answered by on 2012-10-07T16:00:42-04:00
The second parameter of inflate() is the default parent:

_fragmentView = inflater.inflate(R.layout.yes_fragment, container);
Change this line to:

_fragmentView = inflater.inflate(R.layout.yes_fragment, null);
Your Answer
No advertising and No spamming please.