可能重复:
首先在孩子的父母上调用 removeView()
我的活动由 3 个相应的线性布局内的 3 个 Listview 组成。我得到“指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。” 当我尝试在列表视图之间切换时出现错误。当我一个接一个地进行时它工作正常。请帮助我提供一个片段或示例来解决这个问题。谢谢。
我的错误日志是
12-14 15:32:50.017: E/AndroidRuntime(5102): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.ViewGroup.addView(ViewGroup.java:1871)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.ViewGroup.addView(ViewGroup.java:1828)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.ViewGroup.addView(ViewGroup.java:1808)
12-14 15:32:50.017: E/AndroidRuntime(5102): at com.kk.World.TheWorld.getView(TheWorld.java:629)
12-14 15:32:50.017: E/AndroidRuntime(5102): at com.kk.World.TheWorld.onClick(TheWorld.java:428)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.View.performClick(View.java:2538)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.view.View$PerformClick.run(View.java:9152)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.os.Handler.handleCallback(Handler.java:587)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.os.Handler.dispatchMessage(Handler.java:92)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.os.Looper.loop(Looper.java:130)
12-14 15:32:50.017: E/AndroidRuntime(5102): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-14 15:32:50.017: E/AndroidRuntime(5102): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 15:32:50.017: E/AndroidRuntime(5102): at java.lang.reflect.Method.invoke(Method.java:507)
12-14 15:32:50.017: E/AndroidRuntime(5102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-14 15:32:50.017: E/AndroidRuntime(5102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-14 15:32:50.017: E/AndroidRuntime(5102): at dalvik.system.NativeStart.main(Native Method)
我的看法是
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/statelayoutStg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/state_ll"
android:background="@drawable/mesh"
android:orientation="vertical"
android:visibility="invisible" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/citylayoutStg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/city_ll"
android:background="@drawable/mesh"
android:orientation="vertical"
android:visibility="invisible" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000" />
</LinearLayout>