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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q1269463/article/details/58708259

今天遇到了一个问题使用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;
}

猜你喜欢

转载自blog.csdn.net/q1269463/article/details/58708259
今日推荐