Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment (with Compatibility Package on 2.3.3) creates "Specified child already has a parent error"

I'm using fragments in my Android application using the compatibility package. I have tested the application on a Nexus One running 2.3.3 and an Google API 2.2 emulator.

When adding the fragment via the layout XML it works great (using a fragment tag).

When adding the fragment dynamically into a FrameLayout it does not successfully add the fragment to the container and it throws the following error.

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

Full stacktrace for this is below.

05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:578)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1219)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:380)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.os.Handler.handleCallback(Handler.java:587)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.os.Looper.loop(Looper.java:123)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at android.app.ActivityThread.main(ActivityThread.java:3839)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at java.lang.reflect.Method.invoke(Method.java:507)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-17 19:44:03.518: DEBUG/UNHANDLED_EXCEPTION(7735):     at dalvik.system.NativeStart.main(Native Method)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735): FATAL EXCEPTION: main
05-17 19:44:03.528: ERROR/AndroidRuntime(7735): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:578)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1219)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:380)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.os.Handler.handleCallback(Handler.java:587)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.os.Looper.loop(Looper.java:123)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at android.app.ActivityThread.main(ActivityThread.java:3839)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at java.lang.reflect.Method.invoke(Method.java:507)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-17 19:44:03.528: ERROR/AndroidRuntime(7735):     at dalvik.system.NativeStart.main(Native Method)

This is the layout I'm using - the FrameLayout is the container I'm inserting into.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <HorizontalScrollView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:scrollbars="none"
        android:id="@+id/activity_phone_tabbedview_scroll">

        <LinearLayout android:orientation="horizontal"
            android:layout_height="wrap_content" android:layout_width="fill_parent">

            <Button android:text="Something" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:background="@drawable/tabs"
                android:textColor="@color/White" android:paddingLeft="10dp"
                android:paddingRight="10dp" android:paddingTop="10dp"
                android:paddingBottom="10dp" android:textSize="19sp"
                android:onClick="Something" />
            <Button android:layout_width="wrap_content" android:id="@+id/button1"
                android:layout_height="wrap_content" android:paddingLeft="10dp"
                android:textColor="@color/White" android:text="Something"
                android:background="@drawable/tabs" android:paddingBottom="10dp"
                android:textSize="19sp" android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:onClick="Something"></Button>
            <Button android:layout_width="wrap_content" android:id="@+id/button2"
                android:layout_height="wrap_content" android:paddingLeft="10dp"
                android:textColor="@color/White" android:text="Something"
                android:background="@drawable/tabs" android:paddingBottom="10dp"
                android:textSize="19sp" android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:onClick="Something"></Button>

            <Button android:text="Something" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:background="@drawable/tabs"
                android:textColor="@color/White" android:paddingLeft="10dp"
                android:paddingRight="10dp" android:paddingTop="10dp"
                android:paddingBottom="10dp" android:textSize="19sp" 
                android:onClick="Something"/>

        </LinearLayout>

    </HorizontalScrollView>

    <FrameLayout  android:id="@+id/activity_phone_tabbedviewer_fragment_container" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:layout_below="@id/activity_phone_tabbedview_scroll">
    </FrameLayout>

</RelativeLayout>

The code I'm using to actually insert the fragment is below.

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
NewsList newsList = new NewsList();
fragmentTransaction.add(R.id.activity_phone_tabbedviewer_fragment_container, newsList);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();

Does anyone have any idea what is wrong?

like image 337
Tom Avatar asked May 17 '11 18:05

Tom


2 Answers

Can you post the content of your onCreateView(...) method in the NewsList Fragment?

I'm guessing that you are calling LayoutInflater.inflate(...) at some point. If so you need to use the overloaded version and pass attachToRoot as false as the View will be added to the hierachy in the Fragment.replace(...) call, so should not be attached during inflation.

like image 116
crafty Avatar answered Nov 20 '22 06:11

crafty


Thanks a lot! To make it more trivial, here is how this worked for me:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Call the specific overload of inflate
    return inflater.inflate(R.layout.frag_layout, null, false);
}
like image 11
allprog Avatar answered Nov 20 '22 08:11

allprog