I wish i could help, but i don't really know enough about these things. I have created an app that dynamically added items in a loop, where i learnt about addView() and the attributes object (a pain!), but i did not use removeAllViews(). Maybe i can suggest something to help though. You can decide if this is helpful or not.
If i were debugging this, i would use debug (and logs) to make sure the code was executing. I would also whittle the code down to the point where it worked, to find the line of code causing the issue. Especially as it is so easy to create a new project to
test it out. (Close Studio, copy and paste the directory, open Studio.) Are you able to try something like this to pinpoint the line of code causing the issue?