Call removeView() on the child's parent first
Solution:
((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
//scrollView.removeView(scrollChildLayout);
Use the child element to get a reference to the parent. Cast the parent to a ViewGroup so that you get access to the removeView method and use that.
Thanks to @Dongshengcn for the solution
Try remove scrollChildLayout from its parent view first?
scrollview.removeView(scrollChildLayout)
Or remove all the child from the parent view, and add them again.
scrollview.removeAllViews()
In onCreate with activity or in onCreateView with fragment.
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_main, container, false);
} catch (InflateException e) {
}