AHViewPager slider error

SpaceCow

Member
Licensed User
Longtime User
Hi,

I've integrated the AHViewPager slider (http://www.b4x.com/forum/additional...er-library-sliding-panels-now-perfect-10.html) to my app but i have some problems with adding multiple tabs.

The slider works perfect but i'm getting a error on:
B4X:
container.AddPage(pan,d.name)

I'm adding the pages from a loop and the first and second time when i call the function it works but after that i'm getting the message:
"The specified child already had a parent. You must call removeView() on the child's parent first."

I saw this post but i don't know how to fix it...
Do you use the whole code snippet in your for/next loop?

Then you get the error because you add the fixedTabs and the pager multiple times to the activity.

Currently this is my code:
B4X:
Sub ShowSchema
   Dim in As InputStream
   Dim col As ColorDrawable
   
   'Layout
   P_LayoutContent.LoadLayout("SchemaLayout")
   
   parser.Initialize
   container.Initialize
   MyDays.Initialize
   in = File.OpenInput(File.DirInternal, "scheme0.dat")
   parser.Parse(in, "SchemaParser")
   in.Close      
   
   'Read list
   For i = 0 To MyDays.Size - 1
      Dim d As Day
      d = MyDays.Get(i)

      'Tabs
      Dim pan As Panel
      pan = CreatePanel(TYPE_LISTVIEW, d.name, d.Activities)
      container.AddPage(pan,d.name)
   Next    
   
   'Now we have a container with our panels just add it to the pager object
   pager.Initialize(container, "Pager")
   
   'As we want to show the tabs page indicator, we initialize it
   tabs.Initialize(pager)
   tabs.LineHeight = 5dip
   tabs.UpperCaseTitle = True
   P_SchemaContent.AddView(tabs, 0, 0, FILL_PARENT, WRAP_CONTENT)
   
   'We add a line below the tab page indicator because it looks good
   col.Initialize(tabs.LineColorCenter, 0)
   line.Initialize("")
   line.Background=col
   P_SchemaContent.AddView(line, 0, 35dip, P_SchemaContent.Width, 2dip)
   
   'Now we can add the pager to the activity
   P_SchemaContent.AddView(pager, 0, 35dip + 2dip, P_SchemaContent.Width, P_SchemaContent.Height-48dip-35dip-2dip)

   'Thema
   tabs.Color = Colors.Black
   tabs.BackgroundColorPressed = Colors.Blue
   tabs.LineColorCenter = Colors.Green
   tabs.TextColor = Colors.LightGray
   tabs.TextColorCenter = Colors.Green
   line.Color = Colors.Green
End Sub

The Sub ShowSchema is called when i select a menuitem from my app.

Can someone help me? Thanks!
 

SpaceCow

Member
Licensed User
Longtime User
CreatePanel:
B4X:
Sub CreatePanel(PanelType As Int, Title As String, FillData As List) As Panel
   Dim pan As Panel
   Dim pi As PanelInfo
   
   pi.Initialize
   pi.LayoutLoaded = False
   pi.PanelType = PanelType
   pan.Initialize("")
   
   Select PanelType
      Case TYPE_LISTVIEW
         scvMain.Initialize(500)
         LoadDayData(FillData)
         pan.AddView(scvMain, 0, 0, FILL_PARENT, FILL_PARENT)
   End Select 
   
   pan.Tag = pi
   Return pan
End Sub

LoadDayData
B4X:
Sub LoadDayData(FillData As List)
   Dim Bitmap1 As Bitmap
   Dim Panel0 As Panel
   Dim PanelTop, PanelHeight, Label2Top, ProgressBarTop, ProgressBarWidth As Int
   Dim Progress As Int
   
   Bitmap1.Initialize(File.DirAssets,"Icon.png")
   PanelTop=0
   Panel0=scvMain.Panel
   Panel0.Color=Colors.Gray
   
   For i = 0 To FillData.Size - 1
       Dim a As Act
       a = FillData.Get(i)

      
      Dim Panel1 As Panel
      Dim ImageView1 As ImageView
      
       Panel1.Initialize("SchemaLijst")
      Panel1.Tag=i&"0"

      PanelHeight=99dip
      Label2Top=75dip

      Panel0.AddView(Panel1,0,PanelTop,100%y, PanelHeight)
      Panel1.Color=Colors.Black
      
      Dim Label1, Label2 As Label
      Label1.Initialize("SchemaLijst")
      Panel1.AddView(Label1,80dip,5dip,240dip,30dip)
      Label1.Color=Colors.Black
      Label1.Tag=i&"1"
      Label1.Text=a.timeFrom

      ImageView1.Initialize("SchemaLijst")
      ImageView1.Tag=i&"6"
      Panel1.AddView(ImageView1,5dip,5dip,65dip,65dip)
      ImageView1.Bitmap=Bitmap1

      Dim Label3, Label4, Label5 As Label
      Label3.Initialize("SchemaLijst")
      Panel1.AddView(Label3,80dip,30dip,240dip,30dip)
      Label3.Tag=i&"3"
      Label3.Text=a.description
      Label3.TextSize=11

      Label4.Initialize("")
      Panel1.AddView(Label4,10dip,70dip,60dip,30dip)
      Label4.Tag="Label4"
      Label4.Text=""
      Label4.TextSize=11
      Label4.Gravity=Gravity.CENTER

      PanelTop=PanelTop+PanelHeight+1dip
   Next
   
   Panel0.Height=PanelTop
End Sub
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
Like this?
B4X:
   For i = 0 To MyDays.Size - 1
      Dim d As Day
      d = MyDays.Get(i)

      Dim pan As Panel
      pan.Initialize("pan")
      pan.RemoveView
      pan = CreatePanel(TYPE_LISTVIEW, d.name, d.Activities)
      container.AddPage(pan,d.name)
   Next

Still getting the same error :(... I've spent so many hours trying to fix it but i can't figure it out...
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
No, still this: "The specified child already had a parent. You must call removeView() on the child's parent first.". :(

B4X:
   For i = 0 To MyDays.Size - 1
      Dim d As Day
      d = MyDays.Get(i)

      'Tabs aanmaken
      Dim pan As Panel   
      pan = CreatePanel(TYPE_LISTVIEW, d.name, d.Activities)
      pan.RemoveView
      container.AddPage(pan,d.name)
   Next
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
I think the problem is in the container. When i add a clean panel in the AddPage and outcomment the CreatePanel then the error still appears on the AddPage function.

Should i first delete all pages to prevent duplicates?
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
The problem is in your CreatePanel sub. You should run in debug mode and see the exact error line.

When i run the app in debug mode i get the error as described in my first post:
container.AddPage(pan,d.name)

It is very frustrating... May i send you the full code by e-mail so you have a clear look on the app?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the relevant logs:
B4X:
container.AddPage(pan,d.name)


java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
   at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
   at android.view.ViewGroup.addView(ViewGroup.java:1871)
   at android.support.v4.view.ViewPager.addView(ViewPager.java:920)
   at de.amberhome.viewpager.PageContainerAdapter.instantiateItem(PageContainerAdapter.java:86)
   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
   at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)
   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
   at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:710)
   at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2060)
   at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
   at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
   at de.amberhome.viewpager.PageContainerAdapter.notifyDataSetChanged(PageContainerAdapter.java:206)
   at de.amberhome.viewpager.PageContainerAdapter.AddPage(PageContainerAdapter.java:168)
   at com.dieet.main._showmenu_schema(main.java:1806)
   at com.dieet.main._b_schema_click(main.java:603)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:158)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:54)
   at android.view.View.performClick(View.java:2485)
   at android.view.View$PerformClick.run(View.java:9080)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

The error happens inside the view pager library. Maybe corwin42 will be able to give you more information.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Ok, found some minutes to have a look at your code.

I think the problem is in this line in CreatePanel() Sub:

B4X:
pan.AddView(scvMain, 0, 0, FILL_PARENT, FILL_PARENT)

It seems that scvMain is a Activity global variable and you try to add scvMain to multiple panels.
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
Ok,

So i removed the "Dim scvMain As ScrollView" from the Globals and placed it in the CreatePanel:

B4X:
Sub CreatePanel(PanelType As String, Title As String, FillData As List) As Panel
   Dim pan As Panel
   Dim pi As PanelInfo
   Dim scvMain As ScrollView
   
   pi.Initialize
   pi.LayoutLoaded = False
   pi.PanelType = PanelType
   pan.Initialize("")
   
   Select PanelType
      Case "TYPE_SCHEMA"
         scvMain.Initialize(500)
         LoadDayData(FillData, scvMain)
         pan.AddView(scvMain, 0, 0, FILL_PARENT, FILL_PARENT)
      
      Case "TYPE_SCHEMAS"
         scvMain.Initialize(500)
         LoadSchemasData(FillData, scvMain)
         pan.AddView(scvMain, 0, 0, FILL_PARENT, FILL_PARENT)
   End Select 
   
   pan.Tag = pi
   Return pan
End Sub

However i still get the error on: "container.AddPage(pan,d.name)"

Log:
B4X:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
   at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
   at android.view.ViewGroup.addView(ViewGroup.java:1871)
   at android.support.v4.view.ViewPager.addView(ViewPager.java:920)
   at de.amberhome.viewpager.PageContainerAdapter.instantiateItem(PageContainerAdapter.java:86)
   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
   at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)
   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
   at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:710)
   at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2060)
   at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
   at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)

   at de.amberhome.viewpager.PageContainerAdapter.notifyDataSetChanged(PageContainerAdapter.java:206)
   at de.amberhome.viewpager.PageContainerAdapter.AddPage(PageContainerAdapter.java:168)
   at com.dieet.main._jobdone(main.java:978)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
   at anywheresoftware.b4a.keywords.Common$4.run(Common.java:879)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

:sign0085:
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Sorry, I can't see the problem. I use quite similar code in my weather app and I never had such a problem. Are you sure, the pan object is only dimmed locally and not in sub globals?
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
Pan is only used in the subs that create the sliding panels, not in the globals.

Argh, it's so frustrated... i'm stuck with this error for 2 weeks now :(
 
Upvote 0

only1jake

Member
Licensed User
Longtime User
I had the same error and fixed this. It's all to do with where your variables are declared.
Some of the the Pager/container/tabs need to be declared in the sub where you add to the container itself.
Just play around with where you declare your variables.

In my case I had a panel which is declared as a global variable where I added the pager,tabs and container to and just changed the visibility to view it. The issue was that it was declared globally and so it kept trying to reuse the same panel when I did the same method again. I simply made a new panel in the sub and added the pager, tabs and container to that panel then made that panel equal the global panel.
 
Upvote 0
Top