您的位置:首页 > 移动开发 > Android开发

The specified child already has a parent. You must call removeView() on the child's parent first问题

2017-02-28 23:05 453 查看
今天遇到了一个问题使用frangment出现了The specified child already has a parent. You must call removeView() on the child’s parent first的错误,在stackoverflow找到了解决的方法:



private LayoutInflater mInflater;

private WeakReference mRootView = null;



@Override

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)

{

if (inflater != null)

mInflater = inflater;

else

mInflater = LayoutInflater.from(getActivity());

View rootView = mRootView == null ? null : mRootView.get();

if (rootView != null)

{

final ViewParent parent = rootView.getParent();

if (parent != null && parent instanceof ViewGroup)

((ViewGroup) parent).removeView(rootView);

}

else

{

rootView = mInflater.inflate(R.layout.fragment_test, null, false);

mRootView = new WeakReference(rootView);

}

return rootView;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐