View Binding causes "The specified child already has a parent"

Chad Schultz :

I decided to try the new View Binding feature https://developer.android.com/topic/libraries/view-binding, but had the app crash whenever I tried to open the Activity using it. This is the error:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I had already needed to do some troubleshooting and finagling of my various build.gradle dependencies to get the app to even compile and run with View Binding, so I tried various troubleshooting steps to no avail.

Chad Schultz :

I felt stupid when I realized what the problem was. When I converted the Activity to use View Binding, I didn't realize the layout had a View with an id of root.

Changing the ID of that View solved the problem for me. If you're using View Binding, you cannot use root as the ID for any View in your layout, or you will get this unhelpful error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=22827&siteId=1