Android Question [B4X] [XUI] AS DropDownBox - Error when clicking button more than once

Cnrez

Member
Licensed User
Longtime User
[B4X] [XUI] AS DropDownBox - simple in app snackbar/notification/information panel

when I implemented this library with button trigger, when clicked more than once, application Crash and show this error

error:
** Activity (main) Pause event (activity is not paused). **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
asdropdownbox$ResumableSub_Showresume (java line: 339)
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 anywheresoftware.b4a.objects.B4XViewWrapper.AddView(B4XViewWrapper.java:326)
    at com.as.dropdownbox.asdropdownbox$ResumableSub_Show.resume(asdropdownbox.java:339)
    at com.as.dropdownbox.asdropdownbox._vvvvvvvvvvvvvvvvv2(asdropdownbox.java:305)
    at com.as.dropdownbox.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:138)
    at com.as.dropdownbox.b4xmainpage._button1_click(b4xmainpage.java:118)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:5653)
    at android.view.View$PerformClick.run(View.java:22461)
    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:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

this is the code

code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ddb As ASDropDownBox
End Sub

Public Sub Initialize
   
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("frm_main")
    B4XPages.SetTitle(Me,"AS DropDownBox Example")
   
    Sleep(250)
   
    ddb.Initialize(Me,"ddb")
    ddb.Padding = 20dip 'standard is 0
    ddb.ShowAlignment = ddb.ShowAlignment_TOP 'standard is TOP
    ddb.ShowAnimationDuration = 250 'standard is 500
   
    Sleep(3000)
    ddb.Show(Root,"Test text only one line...")
    Sleep(3000)
    ddb.Hide
    Sleep(3000)
    ddb.Show(Root,$"B4A includes all the features needed to quickly develop any type of Android app.
B4A is used by tens of thousands of developers from all over the world, including companies such as NASA, HP, IBM and others.
Together with B4i you can easily develop applications for both Android and iOS.
B4A is 100% free."$)  
   
   
End Sub

Private Sub ddb_Click
    Log("click")
End Sub



Private Sub Button1_Click
    ddb.Show(Root,"Test text only one line...")  
    Sleep(3000)
    ddb.Hide
End Sub

Private Sub Button2_Click
    ddb.Show(Root,$"B4A includes all the features needed to quickly develop any type of Android app.
B4A is used by tens of thousands of developers from all over the world, including companies such as NASA, HP, IBM and others.
Together with B4i you can easily develop applications for both Android and iOS.
B4A is 100% free."$)
    Sleep(3000)
    ddb.Hide
   
End Sub

i attached a modified sample code . please help

regards
 

Attachments

  • AS DropDownBox Example.zip
    10.2 KB · Views: 71

Alexander Stolte

Expert
Licensed User
Longtime User
when clicked more than once, application Crash and show this error
Move the Initialize code to the button, so the view is always recreated when the user presses the button. Then there is no error anymore.

Or write a function where you do the whole thing and only specify the text as a parameter. I have extended your project to it.

If you want to close the DropDownBox after 3 seconds, you can also use the AutoHideMS property, then you save the sleep.

B4X:
Private Sub Button1_Click
    ShowDropDownBox("Test text only one line...")
End Sub

Private Sub ShowDropDownBox(Text As String)
    
    ddb.Initialize(Me,"ddb")
    ddb.AutoHideMs = 3000
    ddb.Padding = 20dip 'standard is 0
    ddb.ShowAlignment = ddb.ShowAlignment_TOP 'standard is TOP
    ddb.ShowAnimationDuration = 250 'standard is 500
    ddb.Show(Root,Text)
    
End Sub
 

Attachments

  • AS DropDownBox Example.zip
    4.7 KB · Views: 74
Upvote 0
Top