Android Question [Solved] CustomLayoutDialog and "child already has a parent" problem.

asales

Expert
Licensed User
Longtime User
I try to show a native ad in the customlayoutdialog.

My code to generate a native ad is based in the code of Erel and it returns a panel.

If I show a custom dialog with ad one time, it works.

The problem is if I call the custom dialog a second time, I get the error:
B4X:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I tried several codes to remove views, etc., but I don't know how to fix it.

In attached there are a small project that reproduces the problem:
- click in the button "Show ad" and you see the custom dialog with the panel.
- close the dialog and click in the button again to see the error.

Thanks in advance for any help.

B4X:
Sub Globals
   Dim pAdmob As Panel
   Private pNativeAd As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   
   pAdmob.Initialize("pAdmob")
   
   pAdmob = CreatePanelNativeAdmob
End Sub

Sub btnEnterDetails_Click
   Dim dialog As CustomLayoutDialog
   Dim sf As Object = dialog.ShowAsync("Title", "Yes", "Cancel", "No", Null, False)
   dialog.SetSize(100%x, 300dip)
   
   Wait For (sf) Dialog_Ready (DialogPanel As Panel)
   
   DialogPanel.LoadLayout("DetailsDialog")
   Dim cd As ColorDrawable
   cd.Initialize(Colors.RGB(Rnd(0,256), Rnd(0,256), Rnd(0,256)), 0)
   pNativeAd.Background = cd
   
   pNativeAd.AddView(pAdmob, 2%x, 2%y, 30%x, 20%y)
   
   Wait For (sf) Dialog_Result (Result As Int)
   
   If Result = DialogResponse.POSITIVE Then
       Dim cs As CSBuilder
       ToastMessageShow(cs.Initialize.Size(30).Color(Colors.Red).Append("Yes!!!").PopAll, True)
   End If
End Sub

Sub CreatePanelNativeAdmob As Panel
   Dim pnl As Panel
   pnl.Initialize("pnl")
   Dim cd As ColorDrawable
   cd.Initialize(Colors.RGB(Rnd(0,200), Rnd(0,200), Rnd(0,200)), 0)
   pnl.Background = cd
   
   Dim lb As Label
   lb.Initialize("")
   lb.TextColor = Colors.White
   lb.textsize = 12
   lb.Text = "NATIVE AD"
   pnl.AddView(lb, 1%x, 1%x, 15%x, 8%y)
   
   Return pnl
End Sub
 

Attachments

  • test_nativead_customdlg.zip
    12.8 KB · Views: 329

asales

Expert
Licensed User
Longtime User
I recommend you to use B4XDialog from XUI Views.

Please post the full error message.

I tried to recreate the sample using B4XDialog and I get the same problem.

This is my new code using B4XDialog:
B4X:
Sub Button1_Click
    ShowDialog
End Sub

Sub ShowDialog
    Dim p As B4XView = XUI.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 100%x, 50%y) 'set the content size
    p.LoadLayout("DetailsDialog")
    
    Dim cd As ColorDrawable
    cd.Initialize(Colors.RGB(Rnd(0,256), Rnd(0,256), Rnd(0,256)), 0)
    pNativeAd.Background = cd
    
    pNativeAd.AddView(pAdmob, 2%x, 2%y, 30%x, 20%y)
    
    Dim rs As ResumableSub = bDialog.Show("Title", "Yes", "No", "Cancel")
    Wait For (rs) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Dim cs As CSBuilder
        ToastMessageShow(cs.Initialize.Size(30).Color(Colors.Red).Append("Yes!!!").PopAll, True)
    End If
End Sub

I think the problem is in this line:
B4X:
pNativeAd.AddView(pAdmob, 2%x, 2%y, 30%x, 20%y)
I create a panel to native ad in Activity_Create, because the native ad takes several seconds to load.
In the first time, it shows, but in the second time I get the error:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main$ResumableSub_ShowDialogresume (java line: 577)
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:4937)
    at android.view.ViewGroup.addView(ViewGroup.java:4768)
    at android.view.ViewGroup.addView(ViewGroup.java:4740)
    at anywheresoftware.b4a.objects.PanelWrapper.AddView(PanelWrapper.java:65)
    at b4a.example.nativepopup.main$ResumableSub_ShowDialog.resume(main.java:577)
    at b4a.example.nativepopup.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv5(main.java:539)
    at b4a.example.nativepopup.main._button1_click(main.java:467)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
** Activity (main) Resume **
This is the error that I get in both codes: B4XDialog and CustomLayoutDialog.

I don't know how to create a panel with the native ad in Activity_Create and show it several times in the custom dialog.

Thanks again for the support.
 
Upvote 0
Top