-4

Possible Duplicate:
Call removeView() on the child’s parent first

My activity consist of 3 Listview inside 3 corresponding linear layouts.I am getting the "The specified child already has a parent. You must call removeView() on the child's parent first." error in it,when I try to toggle among the listview.It is working fine when I go one by other.Kindly help me with a snippet or example to fix on this.Thank you.

My error log is

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)

My view is

    <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>
Community
  • 1
  • 1
Karthik
  • 4,943
  • 19
  • 53
  • 86

2 Answers2

3

I don't think its possible for you to have three listviews with same id @id/android:list for a single ListActivity. And that should be the reason exactly.

If you need to use 3 ListViews then I would suggest you to extend Activity and use Custom Adapters. Since you use same ids it raises the conflict.

But still from my experience having multiple ListView in a same screen is not a good way to design UI.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Thanks @Andro I dont try to load all the three at the same time only one will be available as per selection either City,State or Country.. – Karthik Dec 14 '12 at 11:20
  • its good then. So first thing is you should try to use Custom adapters or use one ListView and based on user selection set the adapter and the data to it. – Andro Selva Dec 14 '12 at 11:22
1

You have 3 listviews with the same android:id you cannot do this.

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent"
    android:cacheColorHint="#00000000" />

Without seeing your inflate getView method I can only guess that it will try to add the first instance of ListView when the view inflates.

Change the id's to something like @+id/list_view_1 @+id/list_view_2 @+id/list_view_3

This should fix the issue.

Also if you are using ListViewActivity or ListViewFragment you will have to use the default class and getViewById() as you would any other view.

Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61