android: java.lang.IllegalStateException: The specified child already has a parent.

I had quite a problem with fragments and the FragmentManager recently. The problem was the following stacktrace, which came when I tried to launch the application:

08-29 18:10:19.761: E/AndroidRuntime(17159): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.access$700(ActivityThread.java:152)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.os.Handler.dispatchMessage(Handler.java:99)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.os.Looper.loop(Looper.java:137)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.main(ActivityThread.java:5328)

08-29 18:10:19.761: E/AndroidRuntime(17159): at java.lang.reflect.Method.invokeNative(Native Method)

08-29 18:10:19.761: E/AndroidRuntime(17159): at java.lang.reflect.Method.invoke(Method.java:511)

08-29 18:10:19.761: E/AndroidRuntime(17159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)

08-29 18:10:19.761: E/AndroidRuntime(17159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)

08-29 18:10:19.761: E/AndroidRuntime(17159): at dalvik.system.NativeStart.main(Native Method)

08-29 18:10:19.761: E/AndroidRuntime(17159): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3306)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3251)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3227)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:914)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.BackStackRecord.run(BackStackRecord.java:682)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.Activity.performStart(Activity.java:5259)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2220)

08-29 18:10:19.761: E/AndroidRuntime(17159): ... 11 more

The problem was the Fragment itself. You actually need to use a different constructor for the inflate method. I forgot to set attachToRoot to “false”, this should fix the problem:

View view = inflater.inflate(R.layout.fragment_graph, container, false);

Yannick Signer