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

异常信息如下:

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)


显示fragment:

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();



解决方法:

在Fragment中的onCreateView中,fragment   view  重复添加了父控件。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Call the specific overload of inflate
    return inflater.inflate(R.layout.frag_layout, null, false);
}

你可能感兴趣的:(specified,child,alre)