Faltenreich / SkeletonLayout

Skeleton view pattern for Android
Apache License 2.0
505 stars 65 forks source link

java.lang.IllegalStateException: The specified child already has a parent. #14

Closed marekdef closed 4 years ago

marekdef commented 4 years ago

I want to use view.createSkeleton().showSkeleton() but if I do that I got exception

Caused by: 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:3936)
        at android.view.ViewGroup.addView(ViewGroup.java:3786)
        at android.view.ViewGroup.addView(ViewGroup.java:3727)
        at android.view.ViewGroup.addView(ViewGroup.java:3700)
        at com.faltenreich.skeletonlayout.SkeletonLayout.<init>(SkeletonLayout.kt:84)
        at com.faltenreich.skeletonlayout.SkeletonLayout.<init>(SkeletonLayout.kt:33)
        at com.faltenreich.skeletonlayout.SkeletonLayoutUtils.createSkeleton(SkeletonExtensions.kt:31)
        at com.faltenreich.skeletonlayout.SkeletonLayoutUtils.createSkeleton$default(SkeletonExtensions.kt:30)
        at pl.senordeveloper.skeletonview.MainActivity.onCreate(MainActivity.kt:14)

My layout is a textView inside ContraintLayout/FrameLayout and I am creating a skeleton in onCreate method (but this also fails if I do postDelayed or in onResume).

I can create a skeleton successfully if I wrap mentioned textview inside xml with SkeletonLayout so I guess I can expect createSkeleton to work in similar fashion (create a wrapper around existing view).

The question is: am I misusing this utility function?

Faltenreich commented 4 years ago

Thank you for reporting this problem!

There is indeed a problem when injecting a SkeletonLayout programmatically into a layout. As you have correctly assumed, the Skeleton serves as a wrapper around the original Views. So in the case of an injected View, we have to replace the original (to be masked) View with a SkeletonLayout which then contains the original View with all of its children.

I am currently working on a bugfix and expect a solution in version 2.0.1 later this day. Thanks again and have a nice day!