`IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.`
同一 `View` 實例被添加到多個父容器,或在 `PagerAdapter.instantiateItem` 中重複添加未移除的 View。
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = views.get(position); // 預建立或緩存的 View
if (view.getParent() != null) {
((ViewGroup) view.getParent()).removeView(view); // 關鍵:先移除
}
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}優先使用 `FragmentStatePagerAdapter` / `FragmentPagerAdapter` 配合 `Fragment`,由系統管理 View 生命周期,避免手動 `removeView` 易出錯。