gpt4 book ai didi

Android Fragment(与 2.3.3 上的兼容包)创建 "Specified child already has a parent error"

转载 作者:IT王子 更新时间:2023-10-29 00:02:42 31 4
gpt4 key购买 nike

我正在使用兼容性包在我的 Android 应用程序中使用 fragment 。我已经在运行 2.3.3 和 Google API 2.2 模拟器的 Nexus One 上测试了该应用程序。

通过布局 XML 添加 fragment 时效果很好(使用 fragment 标签)。

将 fragment 动态添加到 FrameLayout 时,它没有成功将 fragment 添加到容器中,并引发以下错误。

alStateException: 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): 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)

这是我正在使用的布局 - FrameLayout 是我要插入的容器。

<?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>

我用来实际插入 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();

有人知道出了什么问题吗?

最佳答案

你能把你的onCreateView(...)方法的内容贴在NewsList Fragment吗?

我猜你在某个时候调用了 LayoutInflater.inflate(...)。如果是这样,您需要使用 overloaded version并将 attachToRoot 作为 false 传递,因为 View 将被添加到 Fragment.replace(...) 中的层次结构中code> 调用,所以在膨胀过程中不应该附加。

关于Android Fragment(与 2.3.3 上的兼容包)创建 "Specified child already has a parent error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035711/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com