Android Question Error in Generating multiple child panels in a scroll

Sberla

Active Member
Licensed User
Longtime User
Hello I am trying to add more than one panel in a scrollview but the app crash and the error is :

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:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4230)
at anywheresoftware.b4a.objects.PanelWrapper.AddView(PanelWrapper.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Anyone can help ???
the code is as below

B4X:
Dim scrvpanel As ScrollView
Dim Panel1 As Panel
Dim Panelo(10) As Panel
    Dim a, top As Int
    a = 0
       top=0

Sub Activity_Create(FirstTime As Boolean)
scrvpanel.Initialize(100)
    Panel1.Initialize("")
   
    Activity.AddView(Panel1,10dip,179dip,70%x,230dip)

End Sub

Sub cmdGeneraStatistiche_Click

Panel1.AddView(scrvpanel,0,0,100%x,100%y)

For i=0 To miocursore.RowCount -1
                           
                Panelo(a).Initialize("Panel"&a)
               
                miocursore.Position = i
                                                               
                lbl1.Text=miocursore.GetString("a")
                lbl2.Text= miocursore.GetString("b")
                lbl3.Text=miocursore.GetString("c")
                lbl4.Text=spnFornitore.GetItem(miocursore.GetInt("d"))
                lbl5.Text=miocursore.GetInt("e")
                lbl6.Text=miocursore.GetDouble("f")
                lbl7.Text=miocursore.GetDouble("g")
                lbl8.Text=miocursore.GetDouble("h")
                               
                miopanelo
                                                   
                a = a +1
                top = top + 91
               
            Next
   
End Sub

Sub miopanelo
   
    scrvpanel.Panel.AddView(Panelo(a),0,top,100%x,50%y)
   
    Panelo(a).AddView(lbl1,0,0,120dip,10dip)
    Panelo(a).AddView(lbl2,0,11dip,120dip,10dip)
    Panelo(a).AddView(lbl3,0,21dip,120dip,10dip)
    Panelo(a).AddView(lbl4,0,31dip,120dip,10dip)
    Panelo(a).AddView(lbl5,125dip,0,120dip,10dip)
    Panelo(a).AddView(lbl6,125dip,10dip,120dip,10dip)
    Panelo(a).AddView(lbl7,125dip,21dip,120dip,10dip)
    Panelo(a).AddView(lbl8,125dip,32dip,120dip,10dip)
       
    scrvpanel.Panel.Height=scrvpanel.Panel.Height+190
   
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
Try This

B4X:
Dim scrvpanel As ScrollView

Sub Activity_Create(FirstTime As Boolean)
    scrvpanel.Initialize(1000)
    Activity.AddView(scrvpanel,179dip,70%x,230dip)

End Sub

Sub cmdGeneraStatistiche_Click
Dim a, top As Int = 0

scrvpanel.Panel.RemoveAllViews
For i=0 To miocursore.RowCount -1
     Dim Panelo as Panel
     Panelo.Initialize("Panelo")
     Panelo.tag=A
  
      miocursore.Position = i
                                                  
      AddLabel(Panelo,0,miocursore.GetString("a"))
      AddLabel(Panelo,11dip,miocursore.GetString("a"))
      AddLabel(Panelo,21dip,miocursore.GetString("c"))
      AddLabel(Panelo,31,pnFornitore.GetItem(miocursore.GetInt("d"))
      AddLabel(Panelo,41dip,GetDouble.GetString("f"))
      AddLabel(Panelo,51dip,GetDouble.GetString("g"))
      AddLabel(Panelo,61dip,GetDouble.GetString("h"))
                  
      scrvpanel.Panel.AddView(Panelo,0,top,100%x,98dip)
   
      a = a + 1
      top = top + 100dip  
Next
scrvpanel.Panel.Height=Top
End Sub

Sub AddLabel(Pa as panel,Top as int, T as String)
    Dim lbl as Label

    lbl.Text=T
    Pa.AddView(lbl1,0,Top,120dip,10dip)

End Sub

Sub Panelo_Click
Dim Pa as Panel = Sender
Dim NumberPanel as int = Pa.Tag

log("Panel n:" & NumberPanel)
End Sub

Mistakes
B4X:
'1)
Panel1.AddView(scrvpanel,0,0,100%x,100%y) ' Generate error at 2 clic,
'You can not add the same block several times to a panel. before use Panel1.RemoveView

'2)
'You did not dimension the Labels
Not dimensioning new Labels, assign them to other panels and generate this error: "The specified child already has a parent"
 
Last edited:
Upvote 0
Top