关于viewpager里嵌套fragment报 “The specified child already has a parent”的解决办法

  报错内容:“java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.”



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (viewState == null) {
			viewState = inflater.inflate(R.layout.viewpager_state, container,
					false);
		} 
		
		ViewGroup parent = (ViewGroup) viewState.getParent();
		if (parent != null) {
			parent.removeView(viewState);
		}
		return viewState;
	}


在viewpager每个页面的fragment的oncreateview方法里面添加上面判断是否有parent部分代码即可


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页