Android Question DSNavigationDrawer Crashes with IME.AddHeightChangedEvent

JohnC

Expert
Licensed User
Longtime User
If you download the DSNavigationDrawer sample "NavigationView1_0.zip" from the OP here:

https://www.b4x.com/android/forum/t...nal-material-design-components.58893/#content

Then add the IME library to the project.

Then add this line to the "Globals" section:
B4X:
Dim IME As IME        'keyboard

Then add these two lines to the "Activity_Create" right after the "ToolBar.InitMenuListener" line:
B4X:
IME.Initialize("IME")    'keyboard
IME.AddHeightChangedEvent


When you run the sample, you will get this critical error:
B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 367)
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:4212)
    at android.view.ViewGroup.addView(ViewGroup.java:4065)
    at android.view.ViewGroup.addView(ViewGroup.java:4010)
    at android.view.ViewGroup.addView(ViewGroup.java:3986)
    at anywheresoftware.b4a.objects.IME.AddHeightChangedEvent(IME.java:119)
    at example.navigationview.main._activity_create(main.java:367)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at example.navigationview.main.afterFirstLayout(main.java:104)
    at example.navigationview.main.access$000(main.java:17)
    at example.navigationview.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 

Semen Matusovskiy

Well-Known Member
Licensed User
Design Support is really great and very useful library,
But there are obvious problems in communication with B4A core objects.
For instance, I was not able to re-create DSNavigationDrawer on fly (between Activity_Create events).

I think it will be fine, if B4A developers will assist to fix troubles and will make a library as 'official'
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think it will be fine, if B4A developers will assist to fix troubles and will make a library as 'official'
You need to first explain what is missing in jfeinstein sliding menu. It should be very simple to add an image at the top and make it look like DSNavigationDrawer.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Hey Erel,

First, I don't like that slidingmenu because it doesn't slide the menu out in the typical manner of other apps - it slides the existing activity out to the right to expose the menu instead of the typical method of the menu sliding out from the left *on top* of the existing activity.

Second, it too crashes as reported in post #111 of that thread, that I just confirmed gives the same exact error message when used with IME.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim sm As SlidingMenu
    
    Dim IME As IME        'keyboard
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    sm.Initialize("sm")
    Dim offset As Int = 100dip
    sm.BehindOffset = offset
    sm.Mode = sm.BOTH
    Dim lv1 As ListView
    lv1.Initialize("lv1")
    For i = 1 To 10
        lv1.AddSingleLine("Action "  & i)
    Next
    sm.SecondaryMenu.AddView(lv1, 0, 0, 100%x - offset, 100%y)
    Dim b1, b2 As Button
    b1.Initialize("b1")
    b2.Initialize("b2")
    b1.Text = "Button1"
    b2.Text = "Button2"
    sm.Menu.AddView(b1, 10dip, 10dip, 100%x - offset - 20dip, 50dip)
    sm.Menu.AddView(b2, 10dip, 60dip, 100%x - offset - 20dip, 50dip)
    
    IME.Initialize("IME")    'keyboard
    IME.AddHeightChangedEvent
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lv1_ItemClick (Position As Int, Value As Object)
    Log(Value)
    sm.HideMenus
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK And sm.Visible Then
        sm.HideMenus
        Return True
    End If
    Return False
End Sub
Sub B1_Click
    Log("b1_click")
    sm.HideMenus
End Sub
Sub B2_Click
    Log("b2 click")
    sm.HideMenus
End Sub

B4X:
Logger connected to: 1e542c85
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 389)
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:4212)
    at android.view.ViewGroup.addView(ViewGroup.java:4065)
    at android.view.ViewGroup.addView(ViewGroup.java:4010)
    at android.view.ViewGroup.addView(ViewGroup.java:3986)
    at anywheresoftware.b4a.objects.IME.AddHeightChangedEvent(IME.java:119)
    at b4a.example.main._activity_create(main.java:389)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at b4a.example.main.afterFirstLayout(main.java:104)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
If I also init and add the heightchange event of the IME before this line for the DSNavigationDrawer:

B4X:
NavDrawer.Initialize2("NavDrawer", Activity, NavDrawer.DefaultDrawerWidth, NavDrawer.GRAVITY_START)

It will also prevent the crashing, but now the activity is displayed as pure white - no views on it.

Since the IME seems to have a conflict with two different NavDrawers, is there a possible bug in IME?

I ask because I would rather use DSNavigationDrawer because it slides the navdrawer out from the left on top of the current activity the same way as most apps do. But the other SlidingMenu you mention doesn't do this.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I added some statements (see attached file).
IME.AddHeightChangedEvent creates a parent of Activity
NavDrawer.Initialize2 creates a parent of view, which you pass as second parameter.
So , at first it's necessary to execute IME.AddHeightChangedEvent. Then NavDrawer.Initialize2 with Activity.Parent as parameter.
 

Attachments

  • Test.zip
    111.4 KB · Views: 340
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I added some statements (see attached file).
IME.AddHeightChangedEvent creates a parent of Activity
NavDrawer.Initialize2 creates a parent of view, which you pass as second parameter.
So , at first it's necessary to execute IME.AddHeightChangedEvent. Then NavDrawer.Initialize2 with Activity.Parent as parameter.

Thank you - thank worked!
 
Upvote 0
Top